xcode 使用自动布局的 XIB 中的 UIView 始终为 600x600
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27897884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIView in XIB using Autolayout is always 600x600
提问by Nic Hubbard
I created a separate .xib because I wanted to design a UIView
with autolayout
outside of a viewController. I created the .xib, added the UIView
and the constraints, using wCompact hRegular
. Simple.
我创建了一个单独的.xib因为我想设计一个UIView
带autolayout
一个的viewController之外。我创建了 .xib,添加UIView
了约束,使用wCompact hRegular
. 简单的。
Then add it to my viewController
in viewDidLoad
:
然后将其添加到我的viewController
in 中viewDidLoad
:
UIView *header = [[[NSBundle mainBundle] loadNibNamed:@"HeaderSearch" owner:self options:nil] lastObject];
NSLog(@"%@", NSStringFromCGRect(header.frame));
[self.view addSubview:header];
But, when it is added, the frame size is 600x600 and I cannot figure out why.
但是,添加时,帧大小为 600x600,我不知道为什么。
What have I done wrong here that is forcing this strange size?
我在这里做错了什么导致这个奇怪的大小?
采纳答案by nano
You need to uncheck 'Use size classes' in your xib
您需要在您的 xib 中取消选中“使用尺寸等级”
and the view frame size will be the one you set and not 600x600
并且视图框架尺寸将是您设置的尺寸,而不是 600x600
回答by gabbler
Please refer to this.
请参考这个。
header.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:header];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:header attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
[self.view addConstraint:widthConstraint];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:header attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:130.0];
[self.view addConstraint:widthConstraint];
[self.view addConstraint:heightConstraint];
回答by Michal Cichon
I had a similar problem but in my case I didn't like to turn off trait variations or size classes. I also loading some views from XIBs but I use this handy helper category:
我有一个类似的问题,但就我而言,我不喜欢关闭特征变化或大小类。我还从 XIB 加载了一些视图,但我使用了这个方便的助手类别:
@implementation UIView (Initialization)
+ (instancetype)instantiateFromNib {
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:nil options:nil] firstObject];
}
@end
I didn't want to introduce constraints written in a code so added a overriden viewWillLayoutSubviews
method to my view controller like following:
我不想引入以代码编写的约束,因此viewWillLayoutSubviews
向我的视图控制器添加了一个覆盖方法,如下所示:
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self.firstView setFrame:self.view.bounds];
[self.secondView setFrame:self.view.bounds];
}
In my case firstView
and secondView
are just an overlay to the whole view controller.
就我而言firstView
,secondView
它只是整个视图控制器的叠加。
回答by Igor
I think, this is the best of correct ways:
我认为,这是最好的正确方法:
- (void)willMoveToParentViewController:(UIViewController *)parent {
[super willMoveToParentViewController:parent];
self.view.frame = parent.view.frame;
[self.view layoutIfNeeded];
// adjust subviews here
}
View will get correct frame and adjustments will called only once on initialization.
View 将获得正确的帧,并且在初始化时只调用一次调整。