Add button to navigationbar programmatically

后端 未结 12 2008
日久生厌
日久生厌 2020-12-04 12:03

Hi I need to set the button on right side, in navigation bar, programatically , so that if I press the button I will perform some actions. I have created the navigation bar

12条回答
  •  难免孤独
    2020-12-04 12:22

    If you are not looking for a BarButtonItem but simple button on navigationBar then below code works:

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forState:UIControlStateNormal];
    [aButton addTarget:self
                action:@selector(showButtonView:)
      forControlEvents:UIControlEventTouchUpInside];
    aButton.frame = CGRectMake(260.0, 10.0, 30.0, 30.0);
    [self.navigationController.navigationBar addSubview:aButton];
    

提交回复
热议问题