Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用自动布局的XIB中的UIView始终为600x600_Ios_Objective C_Xcode_Autolayout_Xib - Fatal编程技术网

Ios 使用自动布局的XIB中的UIView始终为600x600

Ios 使用自动布局的XIB中的UIView始终为600x600,ios,objective-c,xcode,autolayout,xib,Ios,Objective C,Xcode,Autolayout,Xib,我创建了一个单独的.xib,因为我想在viewController之外设计一个带有autolayout的UIView。我创建了.xib,添加了UIView和约束,使用wCompact hRegular。简单 然后将其添加到viewdiload中的我的viewController: UIView *header = [[[NSBundle mainBundle] loadNibNamed:@"HeaderSearch" owner:self options:nil] lastObject]; N

我创建了一个单独的.xib,因为我想在viewController之外设计一个带有
autolayout
UIView
。我创建了.xib,添加了
UIView
和约束,使用
wCompact hRegular
。简单

然后将其添加到
viewdiload
中的我的
viewController

UIView *header = [[[NSBundle mainBundle] loadNibNamed:@"HeaderSearch" owner:self options:nil] lastObject];
NSLog(@"%@", NSStringFromCGRect(header.frame));
[self.view addSubview:header];
但是,当它被添加时,帧大小是600x600,我不知道为什么

我在这里做错了什么,导致了这个奇怪的尺寸?

请参考这个

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];

您需要在xib中取消选中“使用大小类”

如果是Xcode 8 disable“Use Trait Variations”,则图幅大小将是您设置的,而不是600x600


我也有类似的问题,但在我的情况下,我不喜欢关闭性状变异或大小分类。我还从XIBs加载了一些视图,但我使用了这个方便的助手类别:

@implementation UIView (Initialization)

+ (instancetype)instantiateFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:nil options:nil] firstObject];
}

@end
我不想引入用代码编写的约束,因此在我的视图控制器中添加了一个重写的
viewWillLayoutSubviews
方法,如下所示:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self.firstView setFrame:self.view.bounds];
    [self.secondView setFrame:self.view.bounds];
}

在我的例子中,
firstView
secondView
只是整个视图控制器的覆盖。我认为,这是最好的正确方法:

- (void)willMoveToParentViewController:(UIViewController *)parent {
    [super willMoveToParentViewController:parent];

    self.view.frame = parent.view.frame;
    [self.view layoutIfNeeded];

    // adjust subviews here
}

视图将获得正确的帧,并且在初始化时只调用一次调整。

您希望它的帧大小是多少?您可能会用约束固定其高度和宽度吗?我想屏幕截图显示的是正确的帧大小?@gabbler-我希望它的高度为superview的130高,但不确定我是否可以用单独的xib来实现……我想你可以做到,对最顶部的视图使用“自由形式大小”,并将其高度更改为130。将标题添加为子视图后,必须手动为标题添加约束,才能使其具有130点高度和与其超级视图相同的宽度。在加载nib时,它的大小是您在xib中看到的大小,但是设置了约束后,它的大小将更改为约束大小。但是为什么会出现这个问题呢?我也快发疯了!如果我使用了大小类,那么我应该能够根据大小类设置视图。有什么原因吗?在不同的iPhone情况下,如何管理视图的高度?如果我想使用尺寸等级?!但随后我就失去了“视为”iPad的可能性;这真的是一条路吗?