Add button to navigationbar programmatically

后端 未结 12 1976
日久生厌
日久生厌 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:13
    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
    }
    
    0 讨论(0)
  • 2020-12-04 12:14
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)]autorelease];
    
    -(void)saveAction:(UIBarButtonItem *)sender{
    
    //perform your action
    
    }
    
    0 讨论(0)
  • 2020-12-04 12:15

    Use following code:

    UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStylePlain target:self action:@selector(customBtnPressed)];
    [self.navigationItem setRightBarButtonItem:customBtn];
    
    0 讨论(0)
  • 2020-12-04 12:16

    Hello everyone !! I created the solution to the issue at hand where Two UIInterface orientations are wanted using the UIIMagePicker.. In my ViewController where I handle the segue to the UIImagePickerController

    **I use a..

    -(void) editButtonPressed:(id)sender {
       BOOL editPressed = YES;
        NSUserDefaults *boolDefaults = [NSUserDefaults standardUserDefaults];
        [boolDefaults setBool:editPressed forKey:@"boolKey"];
        [boolDefaults synchronize];
    
        [self performSegueWithIdentifier:@"photoSegue" sender:nil]; 
    
    }
    

    **

    Then in the AppDelegate Class I do the following.

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
        BOOL appDelBool;
        NSUserDefaults *boolDefaults = [NSUserDefaults standardUserDefaults];
        appDelBool = [boolDefaults boolForKey:@"boolKey"];
    
           if (appDelBool == YES)
               return (UIInterfaceOrientationMaskPortrait);
            else
                return UIInterfaceOrientationMaskLandscapeLeft;
    }
    
    0 讨论(0)
  • 2020-12-04 12:20

    Swift version, add this in viewDidLoad:

    var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneButton:")
    navigationItem.rightBarButtonItem = doneButton
    

    And this in your View Controller class

    func doneButton(sender: UIBarButtonItem) {
        println(111)
    }
    
    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题