Add items to NavigationBar (Not using UINavigationController)

后端 未结 5 472
北恋
北恋 2020-12-31 13:07

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 h

相关标签:
5条回答
  • 2020-12-31 13:37

    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.

    0 讨论(0)
  • 2020-12-31 13:40

    Just use your viewController's navigationItem property.

    like this:

     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(updateContent)];
    
    0 讨论(0)
  • 2020-12-31 13:51
    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];
    
    0 讨论(0)
  • 2020-12-31 13:52

    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;
    
    0 讨论(0)
  • 2020-12-31 14:00

    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
    
    0 讨论(0)
提交回复
热议问题