【问题标题】:Xcode 8: UIView is not working as SubviewXcode 8:UIView 不能作为子视图工作
【发布时间】:2016-09-20 10:42:16
【问题描述】:

我有一个 UIViewController 和一个单独的 UIView(MyTopView),我在 UIViewController 上显示。在Xcode 8 中,解决“选择初始设备”提示后,UIViewUIViewController's 视图中没有正确对齐。

这是我的UIViewController

另外,_viewTop 是一个包含 MyTopView 的容器视图

这是我的UIView (MyTopView)

ViewControllerviewWillAppear方法中,

_myTopView = (MyTopView *)_viewTop;

NSLog(@"_myTopView: %@", _myTopView);
NSLog(@"self.view: %@", self.view);

UIView (MyTopView) 的帧不正确

_myTopView: <MyTopView: 0x7f9f5b5089f0; frame = (0 0; 625 832); autoresize = RM+BM; layer = <CALayer: 0x6080002230c0>>
self.view: <UIView: 0x7f9f5b5084f0; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x608000223080>>

约束警告

    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x608000089560 h=-&- v=-&- UIView:0x7fc4c0408da0.height == MyTopView:0x7fc4c0604780.height - 832   (active)>",
    "<NSLayoutConstraint:0x608000088a20 V:|-(20)-[UIView:0x7fc4c0409c90]   (active, names: '|':UIView:0x7fc4c0408da0 )>",
    "<NSLayoutConstraint:0x608000088ed0 V:[UIView:0x7fc4c0409c90]-(20)-|   (active, names: '|':UIView:0x7fc4c0408da0 )>",
    "<NSLayoutConstraint:0x608000087df0 MyTopView:0x7fc4c0604780.height == 150   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000088ed0 V:[UIView:0x7fc4c0409c90]-(20)-|   (active, names: '|':UIView:0x7fc4c0408da0 )>

我期待 UIView 会显示在 ViewController 的 View 部分中。

它曾经在早期版本的 Xcode7 上工作过。

【问题讨论】:

    标签: ios xcode uiview uiviewcontroller


    【解决方案1】:

    UIView(MyTopView) 初始化期间的某个地方,最好在指定的init 方法中,将其translatesAutoresizingMaskIntoConstraints 属性设置为false/NO

    例如

    - (instancetype) initWithCoder: (NSCoder*) aCoder
    {
        if (!(self = [super initWithCoder: aCoder])) return nil;
    
        self.translatesAutoresizingMaskIntoConstraints = NO;
    
        return self;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2014-11-22
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      相关资源
      最近更新 更多