iOS: Positioning navigation bar buttons within custom navigation bar

前端 未结 3 1134
庸人自扰
庸人自扰 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: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];
    

提交回复
热议问题