1
votes

I'm designing a view controller in my storyboard. It contains a navigation bar at the top and a content view. I've set the constraints as

  • Navigation bar's top space to top layout guide = 0
  • Content view fills the remaining space (leading and trailing space to superview = 0 ; bottom space to superview = 0, top space to navigation bar =0)

Because I want to display the status bar, I'm using the top layout guide to make the navigation bar displayed just under it. It works with a standard UINavigationBar.
But I have custom navigation bar : I've created my own class inheriting UINavigationBar.

The problem is : when specifying this custom class for the navigation bar in the Identity Inspector of the interface builder, the bar is not at the right place when running the app.

Is there anything to add or modify in initWitFrame or initWithCoder, or something else to do, so my custom navigation bar will follow the constraints of my storyboard.

EDIT - source added

- (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
If found that function drawRect:is automatically trigged, setting my navigation bar origin to (0,0). If y don't override it, I'm loosing some custom settings of my bar, especially the background color. Any clue?zbMax
Is it working, when you don't overwrite drawRect:? Can you show the source of your custom UINavigationBar?Arek Holko
Yes, it is working when don't override drawRect:. But I'm loosing some point of the customization. I edit my post for source.zbMax

1 Answers

0
votes

Why are you calling [self setFrame:rect];? frame is correctly set up at this point, you shouldn't change it. Also, rect is in an internal coordinate system of the navigation bar.