例如:
在viewController中添加一個(gè)子控制器,并設(shè)置自控制器view的autoresizingMask
[self addChildViewController:childViewController];
childViewController.view.frame = self.view.bounds;
childViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:childViewController.view];
當(dāng)關(guān)閉自控制器的時(shí)候在viewController上將其移除:
[childViewController.view removeFromSuperview];
[childViewController removeFromParentViewController];
第一次操作添加和刪除時(shí)是沒(méi)有問(wèn)題的,但是第二次添加后執(zhí)行removeFromSuperview方法時(shí)就會(huì)報(bào)錯(cuò),
有上述信息可以大概發(fā)現(xiàn)可能是autoresizing的問(wèn)題,所以我將添加子控制器地方的代碼改為:
[self addChildViewController:childViewController];
[self.view addSubview:childViewController.view];
childViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = @{@"child":childViewController.view, @"view":self.view};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[child]-0-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[child]-0-|" options:0 metrics:nil views:views]];
或者是使用一下方式也是可以的:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:<#(nonnull id)#> attribute:<#(NSLayoutAttribute)#> relatedBy:<#(NSLayoutRelation)#> toItem:<#(nullable id)#> attribute:<#(NSLayoutAttribute)#> multiplier:<#(CGFloat)#> constant:<#(CGFloat)#>]]?
此問(wèn)題只出現(xiàn)在7.x系統(tǒng)及以下版本中,8.0以上系統(tǒng)沒(méi)有這個(gè)問(wèn)題,至于為什么,我也不清楚...可能是蘋(píng)果自己的bug吧,8.0以后修復(fù)了而已.所以如果使用autoresizingMask的同學(xué)在8.0以前版本上時(shí),請(qǐng)多留意.