Xcode 8-自动版式问题 [英] Xcode 8 - Autolayouts Issue
问题描述
我正在使用新的Xcode设置基本的UIView子类,并且遇到了奇怪的行为.乍一看,该子类视图似乎可以正常加载:
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];
}
我开始在自己拥有的iPhone 6大小的情节提要中构建另一个容器视图.我的布局有点复杂,只有很少的对象,即使我擅长设置约束,它也发生了变化.
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.
因此,我开始尝试使用简单视图,以查看Xcode升级后问题出在哪里.
So I started to experiment with simple view, to see where the problem is after the Xcode upgrade.
使用2个UIView进行简单练习.
Simple exercise with 2 UIViews.
每个限制的首选项设置都设置为1000,设备设置为iPhone 6s.
The preference setting was set to 1000 to each constrain and device was set to iPhone 6s.
但是iPhone 6/s或我的设备iPhone 6上的结果发生了变化:
But the result on iPhone 6/s or my device iPhone 6 is shifted:
因此,当我开始摆弄时,我发现如果将情节提要设备设置为iPhone SE,它将在iPhone 6模拟器和iPhone 6设备上正确显示内容:
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:
设置错误,现在已经对齐.我是否错过了新的Xcode版本中的内容以及如何使用自动布局和约束?
Wrong setting and now it's aligned. Have I missed something in the new Xcode version and how to work with autolayouts and constrains?
谢谢 答:
推荐答案
如果baseInit
使用UIView和帧大小进行任何操作,则应在viewDidLayoutSubviews
内部调用它.根据视图的约束对视图进行布局后,将调用此方法.
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];
});
}
这篇关于Xcode 8-自动版式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!