自动添加子视图

时间:2014-09-18 09:03:45

标签: iphone ios8 xcode6

我有一个不使用故事板的应用程序,但使用传统的xib。当我将它改编为iPhone6 / +时,我注意到当我从一个视图控制器添加视图作为另一个viewController的视图的子视图时,它被调整大小但它不应该。

我已创建测试应用以重现此问题,因此这里有一些代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
    // Override point for customization after application launch.
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window!.rootViewController = ViewController(nibName:"ViewController", bundle : NSBundle.mainBundle())
    self.window?.makeKeyAndVisible();
    return true
}


class SubViewController: UIViewController
{
     override func viewDidLoad()
    {
         self.view.frame = UIScreen.mainScreen().bounds

         println("viewDidLoad view. frame\(self.view.frame) screen bounds \(UIScreen.mainScreen().bounds)")
               // => viewDidLoad view. frame(0.0,0.0,320.0,568.0) screen bounds (0.0,0.0,320.0,568.0)
}

class ViewController: UIViewController
{
      var subViewController : SubViewController?

     override func viewDidLoad()
     {
          super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.

         self.subViewController = SubViewController(nibName:"SubViewController", bundle : NSBundle.mainBundle())

         self.view.addSubview(self.subViewController!.view)

         println("super view. frame\(self.view.frame) screen bounds \(UIScreen.mainScreen().bounds)")
             //=>super view. frame(0.0,0.0,600.0,600.0) screen bounds (0.0,0.0,320.0,568.0)
     }

     override func didReceiveMemoryWarning() 
     {
         super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
     }
}

我使用XCode6 GM和iOS8 GM。 如果我在subViewController的viewWillAppear中设置了框架,那么它设置好了,但是我还想弄清楚导致这种行为的原因是什么?

1 个答案:

答案 0 :(得分:0)

我找到了关闭该行为的方法,我只需要将我的子视图的AutoresizingMask设置为None:

   [self.view setAutoresizingMask:UIViewAutoresizingNone];

我的建议是,当我将我的子视图添加到viewDidLoad中的另一个视图时,它在那一刻有错误的帧。因此,当设置了适当的superview框架时,它会将自动调整遮罩应用于导致故障的子视图。