简体   繁体   中英

Add items to NavigationBar (Not using UINavigationController)

I have a UIViewController with a UITableView in it, and also added a UINavigationBar. How can I add and "edit" button and a "+" button in that bar programmatically? (I have tried using IB, but the title is always replaced, and not other items are added) I am not using a UINavigationController. is my UIViewController standing alone.

This is what I have tried without success:

UIBarButtonItem *barButton = 
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered 
                                                  target:nil
                                                  action:nil];
UINavigationItem *editItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
[editItem setLeftBarButtonItem:barButton animated:YES];
[navigationBar setItems:[NSArray arrayWithObject:editItem] animated:YES];

Your UIViewController has a navigationItem property. You can set the left and right bar button items with self.navigationItem.leftBarButtonItem = ... and self.navigationItem.rightBarButtonItem = ...

Edit:

OK, I assume you have a reference to your UINavigationBar ? Then I guess you'd add a single UINavigationItem to it:

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"A Title"];
theNavigationBar.items = [NSArray arrayWithObject:item];
[item release]; // or keep this as an instance variable

and then set that item's left and right buttons:

theNavigationBar.topItem.leftBarButtonItem = ...;
theNavigationBar.topItem.rightBarButtonItem = ...;

I haven't tried this, but I think it should work.

UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(theEditMethod:)];      
[viewController.navigationItem setLeftBarButtonItem:leftBarButton animated:NO];
[leftBarButton release];

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(theAddMethod:)];       
[viewController.navigationItem setLeftBarButtonItem:rightBarButton animated:NO];
[rightBarButton release];

Yo do not need to add UINavigationItem to the UINavigationBar. Yo can do as this example:

NSString *backButtonTittle=[NSString stringWithFormat:@"< %@",NSLocalizedString(@"backButton", nil)];
UIBarButtonItem *backCreateAccountNavBarItem=[[UIBarButtonItem alloc]initWithTitle:backButtonTittle style:UIBarButtonItemStylePlain target:self action:@selector(goToBackStep)];
self.createAccountNavBar.topItem.leftBarButtonItem=backCreateAccountNavBarItem;

Just use your viewController's navigationItem property.

like this:

 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(updateContent)];

Navigation Bar items/buttons with actions on Swift 4.2, ios 11, XCode 10

1) With Storyboard go to Editor > Embed In > Navigation Bar

2) In AppDelegate > didFinishLaunchingWithOptions :

UINavigationBar.appearance().barTintColor = UIColor(hexString: "1C9B90")
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]

3) On viewDidLoad for your viewcontroller, create buttons and add to Navigation Bar:

self.navigationController?.navigationBar.topItem?.title = "Title"
self.navigationController?.isNavigationBarHidden = false

//QR Code button
let qrCodeScanButton = UIButton(type: .custom)
qrCodeScanButton.setImage(UIImage(named: "camera"), for: .normal)
qrCodeScanButton.addTarget(self, action: #selector(self.searchWithQRCode), for: .touchUpInside)
let qrCodeScanButtonItem = UIBarButtonItem(customView: qrCodeScanButton)

///LogOut button
let logoutButton = UIButton(type: .custom)
logoutButton.setImage(UIImage(named: "logOut"), for: .normal)
logoutButton.addTarget(self, action: #selector(self.logOut), for: .touchUpInside)
let logoutButtonItem = UIBarButtonItem(customView: logoutButton)

self.navigationController?.navigationBar.topItem?.setRightBarButtonItems([logoutButtonItem, qrCodeScanButtonItem], animated: true)

4) Actions for buttons:

@objc func logout(){
     ///present your Login VC
}

@objc func qrCodeScanButton(){
     ///present your Login VC
}

5) Build and Run

PS. Keep in mind these two differences.

isNavigationBarHidden : A Boolean value that indicates whether the navigation bar is hidden.

self.navigationController?.isNavigationBarHidden

navigationBar.isHidden : The navigation bar managed by the navigation controller.

self.navigationController?.navigationBar.isHidden

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM