Xcode 8 - Autolayouts Issue

与世无争的帅哥 提交于 2019-12-25 08:34:57

问题


I am setting up basic UIView subclass with the new Xcode and came across weird behaviour. The subclassed view appears to load normally at the first glance:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
        if (self) {
            [self baseInit];
}
    return self;
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    [self baseInit];
}

I started to build another container view in storyboards set to size of iPhone 6 as I own one. I had a bit complicated layout with quite few objects and even if I am good at setting constrains, it came out shifted.

So I started to experiment with simple view, to see where the problem is after the Xcode upgrade.

Simple exercise with 2 UIViews.

The preference setting was set to 1000 to each constrain and device was set to iPhone 6s.

But the result on iPhone 6/s or my device iPhone 6 is shifted:

So when I started to fiddle around, I have discovered that if set the storyboard device to iPhone SE, it will show the content correctly on both, iPhone 6 simulator and iPhone 6 device:

Wrong setting and now it's aligned. Have I missed something in the new Xcode version and how to work with autolayouts and constrains?

Thank you A.


回答1:


If baseInit is doing anything with UIViews and frame sizes you should call it inside viewDidLayoutSubviews. This is called after the view has been laid-out according to the view's constraints.

static dispatch_once_t oncePerLoadToken;

- (void)viewDidLoad {
    [super viewDidLoad];
    oncePerLoadToken = 0;
}

- (void)viewDidLayoutSubviews {
    dispatch_once(&oncePerLoadToken, ^{
        [self baseInit];
    });
}


来源:https://stackoverflow.com/questions/39885012/xcode-8-autolayouts-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!