【问题标题】:Set image to background image for view controller and maintain when rotated on ipad将图像设置为视图控制器的背景图像并在 ipad 上旋转时保持
【发布时间】:2014-09-08 06:26:02
【问题描述】:

我正在尝试将图像作为 ipad 应用程序中 viewcontroller 的背景。我使用

进行设置
UIImage *background = [UIImage imageNamed: @"Pilot@2x~ipad.png"];
            UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
            imageView.frame = self.view.bounds;
            [self.view addSubview: imageView];
            [self.view sendSubviewToBack:imageView];

这适用于纵向模式,但是当我将其旋转到横向模式时,会出现图片无法填充的空白区域。我基于其他类似帖子的想法是创建一张新图片,当旋转获取更改时调用它,我尝试了这个

 {    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){

            UIImage *background = [UIImage imageNamed: @"Pilot@2x~ipad.png"];
            UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
            imageView.frame = self.view.bounds;
            [self.view addSubview: imageView];
            [self.view sendSubviewToBack:imageView];



        } else  if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){

            UIImage *background = [UIImage imageNamed: @"PilotLandscape@2x~ipad.png"];
            UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
            imageView.frame = self.view.bounds;
            [self.view addSubview: imageView];
            [self.view sendSubviewToBack:imageView];
        }}

这导致了同样的事情。任何有关如何解决此问题的想法都将不胜感激!

【问题讨论】:

  • 当你将设备横向旋转到纵向或纵向到横向时
  • 当您将设备从横向旋转到纵向或从纵向旋转到横向时调用? if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){ }else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {}
  • 您的应用中是否自动布局?
  • 在您的项目中添加两个不同大小的图像。 1.对于纵向视图-768X1024(retina-1536X2048),2.对于横向视图-1024X768(retina-2048X1536)。现在,在旋转屏幕时更改背景图像(对于纵向图像 No.1 和对于横向图像 No.2)。

标签: ios objective-c ipad uiimageview uiinterfaceorientation


【解决方案1】:

查看view的autosizing设置,尝试NSLog view的坐标,注意uiimageview的UIViewContentModeScale属性。为什么不简单地将 imageview 添加到情节提要中?

【讨论】:

    【解决方案2】:

    工作代码:

    @interface SOViewController (){
        UIImageView *imageView;
    }
    
    @end
    
    -(void)initialOrientationHandling
    {
        //[self.view removeFromSuperview];
    
        if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeLeft ||
            [[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeRight){
    
            imageView.frame = self.view.bounds;
    
            [self.view addSubview: imageView];
            [self.view sendSubviewToBack:imageView];
    
    
        }else if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationPortrait ||
                  [[UIDevice currentDevice]orientation] == UIDeviceOrientationPortraitUpsideDown){
    
            imageView.frame = self.view.bounds;
    
            [self.view addSubview: imageView];
            [self.view sendSubviewToBack:imageView];
    
        }
        else if([[UIDevice currentDevice]orientation]== UIDeviceOrientationFaceUp){
            if(self.view.frame.size.height < 900){
    
            }else{
    
            }
        }
    }
    
    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
    
        [self initialOrientationHandling];
    }
    

    如果不是高度像素化,显然图像会失真。我建议你对不同的旋转使用不同的。

    【讨论】:

    • 那么在 if 我只设置一张图片,在 else if 另一张呢?视图中的这些都没有加载方法正确吗?
    【解决方案3】:

    斯威夫特:

    首先创建两张图片,一张是横向尺寸,另一张是纵向尺寸。

    初始化图像视图

                  var   Imgview = UIImageView()
    

    在 viewWillAppear 方法中,它在视图加载和旋转时改变图像。

         override func viewWillAppear(animated: Bool) {
        Imgview.frame = self.view.frame
    
    
        Imgview.contentMode = UIViewContentMode.ScaleAspectFill
        Imgview.clipsToBounds = true
        view.addSubview(Imgview)
             // get notification while rotating
    
          NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotate", name: UIDeviceOrientationDidChangeNotification, object: nil
        )
    
    
             }
    

    设置图片的旋转功能,在通知中心调用。

                     func rotate()
                  {
    
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {
        Imgview.frame = self.view.frame
        Imgview.image = UIImage(named: "landscapeSizeimage.png")
    
        self.view.sendSubviewToBack(Imgview)
    
    
       }
    
       if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        Imgview.frame = self.view.frame
        Imgview.image = UIImage(named: "PortraitSizeimage.png")
    
        self.view.sendSubviewToBack(Imgview)
    
    
       }}
    

    这对我有用。希望它对某人有所帮助。而且我不知道它是否会创建更多的图像视图,所以我们可以根据您的意愿删除图像视图,而不是bringtosubview。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 2011-07-10
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多