以编程方式向导航栏添加按钮

时间:2010-05-17 09:55:31

标签: objective-c iphone uinavigationbar uibarbuttonitem

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

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

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

12 个答案:

答案 0 :(得分:182)

在我的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,但它在我的代码中工作。

答案 1 :(得分:19)

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
}

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

答案 2 :(得分:9)

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

 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.
}

答案 3 :(得分:6)

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

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

使用onAdd:

func onAdd(sender: AnyObject) {
}

答案 4 :(得分:5)

self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)]autorelease];

-(void)saveAction:(UIBarButtonItem *)sender{

//perform your action

}

答案 5 :(得分:5)

Swift版本,在viewDidLoad中添加:

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

这在View Controller类中

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

答案 6 :(得分:4)

使用以下代码:

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

答案 7 :(得分:3)

像这样简单地使用本机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];
    }
}

玩得开心...... !!!

答案 8 :(得分:3)

在ViewController.m的ViewDidLoad方法中

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

[self.navigationItem setLeftBarButtonItem:cancel];

“(返回)”选择器是一种解散当前ViewController的方法

答案 9 :(得分:2)

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

以下是代码: -

  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.
  }

答案 10 :(得分:0)

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

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

答案 11 :(得分:-1)

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

**我用了..

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