美文网首页
iOS-关于autoresizingMask在7.x及以下版本的

iOS-关于autoresizingMask在7.x及以下版本的

作者: JohnCA | 来源:发表于2016-07-12 11:06 被阅读38次

例如:

在viewController中添加一个子控制器,并设置自控制器view的autoresizingMask

[self addChildViewController:childViewController];

childViewController.view.frame = self.view.bounds;

childViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:childViewController.view];

当关闭自控制器的时候在viewController上将其移除:

[childViewController.view removeFromSuperview];

[childViewController removeFromParentViewController];

第一次操作添加和删除时是没有问题的,但是第二次添加后执行removeFromSuperview方法时就会报错,

有上述信息可以大概发现可能是autoresizing的问题,所以我将添加子控制器地方的代码改为:

[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)#>]] 

此问题只出现在7.x系统及以下版本中,8.0以上系统没有这个问题,至于为什么,我也不清楚...可能是苹果自己的bug吧,8.0以后修复了而已.所以如果使用autoresizingMask的同学在8.0以前版本上时,请多留意.

相关文章

网友评论

      本文标题:iOS-关于autoresizingMask在7.x及以下版本的

      本文链接:https://www.haomeiwen.com/subject/qlnjjttx.html