【问题标题】:Add button to navigationbar programmatically以编程方式将按钮添加到导航栏
【发布时间】:2011-02-20 08:11:28
【问题描述】:

您好,我需要在右侧的导航栏中以编程方式设置按钮,这样如果我按下按钮,我将执行一些操作。我以编程方式创建了导航栏;

navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];

同样,我需要在此导航栏的右侧添加按钮。对于我使用的,

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];

我尝试了所有这些方法,但都没有显示右侧的按钮。

【问题讨论】:

    标签: objective-c iphone uinavigationbar uibarbuttonitem


    【解决方案1】:

    在我的UIViewController 派生类中,我在viewDidLoad 中使用以下内容:

    UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] 
                                   initWithTitle:@"Flip"                                            
                                   style:UIBarButtonItemStyleBordered 
                                   target:self 
                                   action:@selector(flipView:)];
    self.navigationItem.rightBarButtonItem = flipButton;
    [flipButton release];
    

    这会在右侧添加一个标题为 Flip 的按钮,它会调用该方法:

    -(IBAction)flipView

    这看起来很像你#3,但它在我的代码中工作。

    【讨论】:

    • 我需要在选择器中添加一个冒号 - "action:@selector(flipView:)];
    【解决方案2】:
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)]autorelease];
    
    -(void)saveAction:(UIBarButtonItem *)sender{
    
    //perform your action
    
    }
    

    【讨论】:

      【解决方案3】:

      使用以下代码:

      UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStylePlain target:self action:@selector(customBtnPressed)];
      [self.navigationItem setRightBarButtonItem:customBtn];
      

      【讨论】:

        【解决方案4】:

        像这样简单使用原生的editBarButton

        self.navigationItem.rightBarButtonItem = self.editButtonItem;
        [self.navigationItem.rightBarButtonItem setAction:@selector(editBarBtnPressed)];
        

        然后

        - (void)editBarBtnPressed {
            if ([infoTable isEditing]) {
                [self.editButtonItem setTitle:@"Edit"];
                [infoTable setEditing:NO animated:YES];
            }
            else {
                [self.editButtonItem setTitle:@"Done"];
                [infoTable setEditing:YES animated:YES];
            }
        }
        

        玩得开心……!!!

        【讨论】:

          【解决方案5】:
          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];
          

          ///// 调用事件

          -(IBAction)Back_btn:(id)sender
          {
              //Your code here
          }
          

          斯威夫特:

          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
          }
          

          【讨论】:

            【解决方案6】:

            在 ViewController.m 的 ViewDidLoad 方法中

            UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
            
            [self.navigationItem setLeftBarButtonItem:cancel];
            

            "(back)"选择器是解除当前ViewController的方法

            【讨论】:

              【解决方案7】:

              大家好!!我为手头的问题创建了解决方案,即使用 UIIMagePicker 需要两个 UIInterface 方向。在我处理与 UIImagePickerController 的 segue 的 ViewController 中

              **我用一个..

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

              **

              然后在 AppDelegate 类中我执行以下操作。

              - (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;
              }
              

              【讨论】:

              • 抱歉问题是什么!! :P
              【解决方案8】:

              要在导航栏上添加搜索按钮,请使用以下代码:

               UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(toggleSearch:)];
              self.navigationController.navigationBar.topItem.rightBarButtonItem = searchButton;
              

              并实现以下方法:

              - (IBAction)toggleSearch:(id)sender
              {
                  // do something or handle Search Button Action.
              }
              

              【讨论】:

              • 非常感谢您的解决方案。 topItem 是我错过的东西
              【解决方案9】:

              Swift 版本,在 viewDidLoad 中添加:

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

              这在你的视图控制器类中

              func doneButton(sender: UIBarButtonItem) {
                  println(111)
              }
              

              【讨论】:

                【解决方案10】:

                如何在 swift 中向导航栏添加添加按钮:

                self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onAdd:")
                

                添加:

                func onAdd(sender: AnyObject) {
                }
                

                【讨论】:

                  【解决方案11】:

                  试试这个。它对我有用。 以编程方式将按钮添加到导航栏,我们还将图像设置为导航栏按钮,

                  下面是代码:-

                    UIBarButtonItem *Savebtn=[[UIBarButtonItem alloc]initWithImage:
                    [[UIImage imageNamed:@"bt_save.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
                    style:UIBarButtonItemStylePlain target:self action:@selector(SaveButtonClicked)];
                    self.navigationItem.rightBarButtonItem=Savebtn;
                  
                    -(void)SaveButtonClicked
                    {
                      // Add save button code.
                    }
                  

                  【讨论】:

                    【解决方案12】:

                    如果您不是在寻找 BarButtonItem 而是在 navigationBar 上寻找简单的按钮,那么下面的代码可以工作:

                    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];
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2017-10-28
                      • 1970-01-01
                      相关资源
                      最近更新 更多