Customized navigation bar does not display correctly after returning to Tab

I have added several components to the NavigationBar in the application that I am creating. The height of the panel changes dynamically, as in Safari for iPhone, when you click on the URL field. I have all the interactions and changes that work perfectly, except when I switch to another tab and come back.

If I started the application, click on the second tab, which displays the navigation bar.

navbar OK

If I immediately click on the first tab, and then again on the second tab, then the NavigationBar is cropped.

navbar clipped

I configure navbar in my viewWillAppear method

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (navBar == nil) { CGFloat width = self.view.frame.size.width; navBar = [self.navigationController.navigationBar autorelease]; navBar.frame = CGRectMake(0,20,width,52); //This doesn't appear to have any effect- was set to //UIViewAutoresizingFlexibleWidth and worked but with with the same issue. navBar.autoresizingMask = UIViewAutoresizingFlexibleHeight; self.navigationItem.title = nil; label = [[UILabel alloc] initWithFrame: CGRectMake(10,2,width-20,14)]; label.autoresizingMask = UIViewAutoresizingFlexibleWidth; label.text = @"My Custom NavBar"; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont systemFontOfSize:12]; label.textAlignment = UITextAlignmentCenter; [navBar addSubview:label]; urlTextField = [[UITextField alloc] initWithFrame: CGRectMake(10,19,width-75,26)]; urlTextField.clearButtonMode = UITextFieldViewModeWhileEditing; [urlTextField addTarget:(id)self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; bookmarkButton = [UIButton buttonWithType:UIButtonTypeCustom]; bookmarkButton.frame = CGRectMake(0,0,21,21); [bookmarkButton setImage:[UIImage imageNamed:@"bookmark.png"] forState:UIControlStateNormal]; [bookmarkButton addTarget:self action:@selector(bookmarkPressed:) forControlEvents:UIControlEventTouchUpInside]; urlTextField.leftView = bookmarkButton; urlTextField.leftViewMode = UITextFieldViewModeAlways; actionButton = [UIButton buttonWithType:UIButtonTypeCustom]; actionButton.frame = CGRectMake(0,0,21,21); [actionButton setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal]; [actionButton addTarget:self action:@selector(actionPressed:) forControlEvents:UIControlEventTouchUpInside]; urlTextField.rightView = actionButton; urlTextField.rightViewMode = UITextFieldViewModeAlways; urlTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth; urlTextField.borderStyle = UITextBorderStyleRoundedRect; urlTextField.font = [UIFont systemFontOfSize:17]; urlTextField.delegate = self; [navBar addSubview:urlTextField]; } else { //XXX This does make the navBar display correctly //but I don't really want the keyboard visible when returning from another tab //[urlTextField becomeFirstResponder]; } } 

Check out the final comment in the code above. If I make the keyboard display or the user clicks on a UITextField, the NavBar displays correctly.

I tried a lot of things - checking the frame sizes in the view (view 2 in the images), NavBar, but no luck. Does anyone know what the problem is? I will be happy to tell you more.

Thanks.

+4
source share
1 answer

Basically, you are doing this all wrong. You will never have to access the frame of the navigation panel (not to mention changing it), and you, of course, will never add a subspecies to it. From the docs:

Unlike other types of views, you do not add subviews to the navigation bar directly. Instead, you use a navigation element (an instance of UINavigationItem) to indicate which buttons or custom views you want displayed.

So, in - (id) initWithNibName: bundle: the method of your view controller that you want:

 // Setting this will automatically grow the height of the navigation bar // to the appropriate height self.navigationItem.prompt = @"My Custom NavBar"; // Construct urlTextField here ... self.navigationItem.titleView = urlTextField; // This is the button with the gears icon UIBarButtomItem *rightButtonItem = [[UIBarButtomItem alloc] initWithCustomView:view]; self.navigationItem.rightBarButtonItem = rightButtomItem; [rightButtomItem release]; 
+1
source

Source: https://habr.com/ru/post/1302626/


All Articles