使用iOS 7顶级布局指南

时间:2013-10-17 08:43:29

标签: ios storyboard uinavigationbar autolayout

我正在设计故事板中的视图控制器。它包含顶部的导航栏和内容视图。我已将约束设置为

  • 导航栏的顶部空间到顶部布局指南= 0
  • 内容视图填充剩余空间(前导和尾随空格为superview = 0;底部空间为superview = 0,顶部空间为导航栏= 0)

因为我想显示状态栏,所以我使用顶部布局指南使导航栏显示在其下方。它适用于标准的UINavigationBar。
但我有自定义导航栏:我创建了自己的继承UINavigationBar的类。

问题是:在界面构建器的Identity Inspector中为导航栏指定此自定义类时,在运行应用程序时,该栏不在正确的位置。

是否需要在initWitFrameinitWithCoder中添加或修改任何内容,或其他要做的事情,因此我的自定义导航栏将遵循故事板的限制。

编辑 - 已添加来源

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // additional setup
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:(NSCoder *)aDecoder];
    if (self) {
        // same setup than initWithFrame:
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    //[self setFrame:rect]; navigation bar is not at the right place if uncommented
}

1 个答案:

答案 0 :(得分:0)

你为什么打电话给[self setFrame:rect];?此时frame已正确设置,您不应更改它。此外,rect位于导航栏的内部坐标系中。