How to add a right button to a UINavigationController?

后端 未结 21 989
不思量自难忘°
不思量自难忘° 2020-11-28 18:25

I am trying to add a refresh button to the top bar of a navigation controller with no success.

Here is the header:

@interface PropertyViewController          


        
相关标签:
21条回答
  • 2020-11-28 18:34

    Try adding the button to the navigationItem of the view controller that is going to be pushed onto this PropertyViewController class you have created.

    That is:

    MainViewController *vc = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
    vc.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];
    
    PropertyViewController *navController = [[PropertyViewController alloc] initWithRootViewController:vc];
    

    Now, this infoButton that has been created programatically will show up in the navigation bar. The idea is that the navigation controller picks up its display information (title, buttons, etc) from the UIViewController that it is about to display. You don't actually add buttons and such directly to the UINavigationController.

    0 讨论(0)
  • 2020-11-28 18:34
    -(void) viewWillAppear:(BOOL)animated
    {
    
        UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnRight setFrame:CGRectMake(0, 0, 30, 44)];
        [btnRight setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
        [btnRight addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *barBtnRight = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
        [barBtnRight setTintColor:[UIColor whiteColor]];
        [[[self tabBarController] navigationItem] setRightBarButtonItem:barBtnRight];
    
    }
    
    0 讨论(0)
  • 2020-11-28 18:34

    Just copy and paste this Objective-C code.

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [self addRightBarButtonItem];
    }
    - (void) addRightBarButtonItem {
        UIButton *btnAddContact = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [btnAddContact addTarget:self action:@selector(addCustomerPressed:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:btnAddContact];
        self.navigationItem.rightBarButtonItem = barButton;
    }
    
    #pragma mark - UIButton
    - (IBAction)addCustomerPressed:(id)sender {
    // Your right button pressed event
    }
    
    0 讨论(0)
  • 2020-11-28 18:36

    Why are you subclasses UINavigationController? There is no need to subclass it if all you need to do is add a button to it.

    Set up a hierarchy with a UINavigationController at the top, and then in your root view controller's viewDidLoad: method: set up the button and attach it to the navigation item by calling

    [[self navigationItem] setRightBarButtonItem:myBarButtonItem];
    
    0 讨论(0)
  • 2020-11-28 18:39

    You should add your barButtonItem in - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated method.

    0 讨论(0)
  • 2020-11-28 18:40

    Swift 4 :

    override func viewDidLoad() {
        super.viewDidLoad()
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "tap me", style: .plain, target: self, action: #selector(onButtonTap))
    }
    
    @objc func onButtonTap() {
        print("you tapped me !?")
    }
    
    0 讨论(0)
提交回复
热议问题