【发布时间】:2017-12-15 05:17:16
【问题描述】:
我在添加子视图时遇到问题。
我有一个可以正常工作的主 xib,我想在其中插入另一个 xib 的子视图。
interface builder of subview xib with constraints
what I see in simulator in smaller iPhones
这两个 xib 都是为 iPhone 7plus 设计的,我在 iPhone 7plus 模拟器中看到它们没问题,但是当我选择 SE 时,我看到主 xib 完美(在所有 iPhone 中),以及我在代码中导入的第二个子视图 xib,看起来比屏幕大。
(如果在 Interface builder 中,对于 de subview xib 我选择 iPhone SE,那么我在 iPhone SE 模拟器中看到它正确,但在 iPhone 7plus 模拟器中更小)
我尝试了很多具有自动布局的常量,但都不起作用。我认为使用 insertSubview 时一定是代码问题。
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Crea vista Informacion
[self creaVistaInformacion];
}
-(void) creaVistaInformacion{
UIView *viewInfoNib = [[[NSBundle mainBundle] loadNibNamed:@"AvisosInformacion" owner:self options:nil] objectAtIndex:0];
[[self view] insertSubview:viewInfoNib atIndex:0];
}
如果你能帮助我,谢谢!
【问题讨论】:
-
您需要根据父视图的边界设置 viewInfoNib 的框架。但更好的做法是为您的新子视图创建约束,使视图可根据设备调整大小
-
我有编辑屏幕截图,您可以在其中看到约束。如果我在主 xib 中使用它们,它们可以正常工作,但是当我在使用 insertSubview 添加的第二个 xib 中使用它们时它们不会
标签: ios objective-c xcode autolayout xib