基于方向的UIView背景图像

时间:2013-03-02 13:16:59

标签: iphone ipad ios6 uiinterfaceorientation

如何在ios6 for iphone和ipad中为不同方向设置不同的背景图像?现在我将图像bg设置为垂直,如下所示,

- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ihome.png"]];
[super viewDidLoad];
}

我厌倦了以下编码,甚至没有进入代码

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (toInterfaceOrientation==UIInterfaceOrientationMaskLandscapeRight)
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lhomescreen.png"]];
}
if (toInterfaceOrientation==UIInterfaceOrientationMaskLandscapeLeft)
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lhomescreen.png"]];
}
if (toInterfaceOrientation==UIInterfaceOrientationMaskPortrait)
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ihome.png"]];
}

return YES;

}

请帮我解决一下

修改

- (BOOL)shouldAutorotate
{
return YES;
}

 - (NSUInteger)supportedInterfaceOrientations
  {
// return (UIInterfaceOrientationMaskAllButUpsideDown);
 return (UIInterfaceOrientationMaskAll);
 }

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

if (toInterfaceOrientation==UIInterfaceOrientationMaskLandscapeRight)

          self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lhomescreen.png"]];

if (toInterfaceOrientation==UIInterfaceOrientationMaskLandscapeLeft)

         self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lhomescreen.png"]];

if (toInterfaceOrientation==UIInterfaceOrientationMaskPortrait)

       self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ihome.png"]];


  // return YES;
 }

1 个答案:

答案 0 :(得分:0)

来自apple doc:

shouldAutorotateToInterfaceOrientation: 返回一个布尔值,指示视图控制器是否支持指定的方向。 (在iOS 6.0中不推荐使用。改为覆盖supportedInterfaceOrientationspreferredInterfaceOrientationForPresentation方法。)“

- (BOOL)shouldAutorotate
{
if (toInterfaceOrientation==UIInterfaceOrientationMaskLandscapeRight)

      self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lhomescreen.png"]];

if (toInterfaceOrientation==UIInterfaceOrientationMaskLandscapeLeft)

     self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lhomescreen.png"]];

if (toInterfaceOrientation==UIInterfaceOrientationMaskPortrait)

   self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ihome.png"]];


return YES;
}