Adding Navigation Bar programmatically iOS

前端 未结 5 2143
你的背包
你的背包 2020-11-29 03:48

I am trying to make an App with a Navigation Bar (Back button, title, etc) and a Tab Bar (Toolbar on the bottom). I am using subviews so I don\'t have to worry about the sta

5条回答
  •  日久生厌
    2020-11-29 04:09

    -(void)ViewDidLoad
    {
     UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    
    UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:@"karthik"];
    // [navbar setBarTintColor:[UIColor lightGrayColor]];
    UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
    navItem.leftBarButtonItem = cancelBtn;
    
    UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)];
    navItem.rightBarButtonItem = doneBtn;
    
    [navbar setItems:@[navItem]];
    [self.view addSubview:navbar];
    }
    
    
    -(void)onTapDone:(UIBarButtonItem*)item{
    
    }
    
    -(void)onTapCancel:(UIBarButtonItem*)item{
    
    }
    

    Swift3

    let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height:44)) // Offset by 20 pixels vertically to take the status bar into account
    
        navigationBar.backgroundColor = UIColor.white
    
    
        // Create a navigation item with a title
        let navigationItem = UINavigationItem()
        navigationItem.title = "Title"
    
        // Create left and right button for navigation item
         let leftButton =  UIBarButtonItem(title: "Save", style:   .plain, target: self, action: #selector(ViewController.btn_clicked(_:)))
    
        let rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: nil)
    
        // Create two buttons for the navigation item
        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton
    
        // Assign the navigation item to the navigation bar
        navigationBar.items = [navigationItem]
    
        // Make the navigation bar a subview of the current view controller
        self.view.addSubview(navigationBar)
    
    
    
    
    func btn_clicked(_ sender: UIBarButtonItem) {
        // Do something
    }
    

    Swift

     // Create the navigation bar
        let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account
    
        navigationBar.backgroundColor = UIColor.whiteColor()
        navigationBar.delegate = self;
    
        // Create a navigation item with a title
        let navigationItem = UINavigationItem()
        navigationItem.title = "Title"
    
        // Create left and right button for navigation item
        let leftButton =  UIBarButtonItem(title: "Save", style:   UIBarButtonItemStyle.Plain, target: self, action: "btn_clicked:")
        let rightButton = UIBarButtonItem(title: "Right", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
    
        // Create two buttons for the navigation item
        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton
    
        // Assign the navigation item to the navigation bar
        navigationBar.items = [navigationItem]
    
        // Make the navigation bar a subview of the current view controller
        self.view.addSubview(navigationBar)
    
    
      func btn_clicked(sender: UIBarButtonItem) {
      // Do something
     }
    

提交回复
热议问题