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, programatically by;
navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];
Similarly, I need to add the button, on the right side of this navigation bar. For that I used,
1.
UIView* container = [[UIView alloc] init]; // create a button and add it to the container UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 130, 44.01)]; [container addSubview:button]; [button release]; // add another button button = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 50, 44.01)]; [container addSubview:button]; [button release]; // now create a Bar button item UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container]; // set the nav bar's right button item self.navigationItem.rightBarButtonItem = item; [item release];
2.
UIImage *im; im=[UIImage imageNamed:@"back.png"]; [button setImage:im forState:UIControlStateNormal]; [im release]; backButton = [[UIBarButtonItem alloc] initWithCustomView:button]; [backButton setImageInsets:UIEdgeInsetsMake(0, -10,5, 5)]; [self.navigationItem setRightBarButtonItem:backButton];
3.
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"button" style:UIBarButtonItemStylePlain target:self action:@selector(refreshLogsAction:)]; self.navigationItem.rightBarButtonItem = refreshItem; [refreshItem release];
I tried all these ways, but none is displaying the button on the right hand side.
From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen.
Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.
A Navigation Item represents the items that are displayed by a navigation bar so that the user can interact and navigate to other view controllers in the iOS applications. It is an instance of UINavigationItem class which inherits NSObject class.
Inside my UIViewController
derived class, I am using the following inside viewDidLoad
:
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle:@"Flip" style:UIBarButtonItemStyleBordered target:self action:@selector(flipView:)]; self.navigationItem.rightBarButtonItem = flipButton; [flipButton release];
This adds a button to the right hand side with the title Flip, which calls the method:
-(IBAction)flipView
This looks very much like you #3, but it is working within my code.
UIImage* image3 = [UIImage imageNamed:@"back_button.png"]; CGRect frameimg = CGRectMake(15,5, 25,25); UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg]; [someButton setBackgroundImage:image3 forState:UIControlStateNormal]; [someButton addTarget:self action:@selector(Back_btn:) forControlEvents:UIControlEventTouchUpInside]; [someButton setShowsTouchWhenHighlighted:YES]; UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton]; self.navigationItem.leftBarButtonItem =mailbutton; [someButton release];
///// called event
-(IBAction)Back_btn:(id)sender { //Your code here }
SWIFT:
var image3 = UIImage(named: "back_button.png") var frameimg = CGRect(x: 15, y: 5, width: 25, height: 25) var someButton = UIButton(frame: frameimg) someButton.setBackgroundImage(image3, for: .normal) someButton.addTarget(self, action: Selector("Back_btn:"), for: .touchUpInside) someButton.showsTouchWhenHighlighted = true var mailbutton = UIBarButtonItem(customView: someButton) navigationItem?.leftBarButtonItem = mailbutton func back_btn(_ sender: Any) { //Your code here }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With