iOS: Positioning navigation bar buttons within custom navigation bar

前端 未结 3 1133
庸人自扰
庸人自扰 2020-12-17 20:22

I\'m building an app with a custom navigation bar. After some research I decided to do this using a category on UINavigationBar. The navigation bar needs to be a bit larger

相关标签:
3条回答
  • 2020-12-17 21:14

    My solution, not the best, but it works for me fine. My custom navigation bar has height 55 (default height is 44). I cut from my custom navigation bar only 44 of height and insert it to the navigation bar. Then I cut next part of my custom navigation bar (shadows etc.) and insert it as image view under the navigation bar. And that's it. Buttons are at right places...

    0 讨论(0)
  • 2020-12-17 21:24

    You'll need to add the leftBarButtonItem and rightBarButtonItem as custom items and mess with the frames.... for example:

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,5,buttonImage.size.width,buttonImage.size.height)];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button addTarget:delegate action:@selector(barButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:titleString forState:UIControlStateNormal];
    [button setTitleColor:CUSTOM_BAR_BUTTON_TITLE_COLOR forState:UIControlStateNormal];
    [[button titleLabel] setFont:[UIFont boldSystemFontOfSize:14]];
    [[button titleLabel] setShadowColor:CUSTOM_BAR_BUTTON_SHADOW_COLOR];
    [[button titleLabel] setShadowOffset:CGSizeMake(0,-1)];
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button release];
    
    [[self navigationItem] setRightBarButtonItem:barButton];
    [barButton release];
    
    0 讨论(0)
  • 2020-12-17 21:25

    Try adding the buttons to the nav bar in the viewDidLoad method of the view controller instead.

    0 讨论(0)
提交回复
热议问题