【问题标题】:Adding subview designed for iPhone 7 looks big in smaller iPhone添加为 iPhone 7 设计的子视图在较小的 iPhone 中看起来很大
【发布时间】: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


【解决方案1】:

您在 nib 描述的视图中有内部约束,但在 nib 外部没有约束,可以在您的超级视图中定位和调整它的大小(witch 是您的视图控制器的视图)。

一个使用框架的例子:

UIView *viewInfoNib = [[[NSBundle mainBundle] loadNibNamed:@"AvisosInformacion" owner:self options:nil] objectAtIndex:0];
// Setting the right frame (will not work if the view is resized)
[viewInfoNib setFrame:self.view.bounds];
[[self view] insertSubview:viewInfoNib atIndex:0];

一个使用约束的例子:

UIView *viewInfoNib = [[[NSBundle mainBundle] loadNibNamed:@"AvisosInformacion" owner:self options:nil] objectAtIndex:0];

[[self view] insertSubview:viewInfoNib atIndex:0];

// Create here the constraints (will work even if the view is resized)
// You can create then with the visual format for example, check http://commandshift.co.uk/blog/2013/01/31/visual-format-language-for-autolayout/

【讨论】:

  • 非常感谢!使用框架的第一个示例运行良好,现在我看到每部 iPhone 一切正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多