以编程方式将导航栏添加到导航栏

mac*_*mac 87 iphone objective-c uinavigationbar uibarbuttonitem

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

navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];
Run Code Online (Sandbox Code Playgroud)

同样,我需要在此导航栏的右侧添加按钮.为此我用过,

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];
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

3.  

    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"button"               style:UIBarButtonItemStylePlain target:self action:@selector(refreshLogsAction:)];
    self.navigationItem.rightBarButtonItem = refreshItem;

    [refreshItem release];
Run Code Online (Sandbox Code Playgroud)

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

Xet*_*ius 185

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

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

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

-(IBAction)flipView

这看起来非常像你#3,但它在我的代码中工作.


Bha*_*ayi 22

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];
Run Code Online (Sandbox Code Playgroud)

/////叫做事件

-(IBAction)Back_btn:(id)sender
{
    //Your code here
}
Run Code Online (Sandbox Code Playgroud)

迅速:

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
}
Run Code Online (Sandbox Code Playgroud)


Jab*_*bar 9

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

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

并实现以下方法:

- (IBAction)toggleSearch:(id)sender
{
    // do something or handle Search Button Action.
}
Run Code Online (Sandbox Code Playgroud)


raf*_*raf 6

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

self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onAdd:")
Run Code Online (Sandbox Code Playgroud)

使用onAdd:

func onAdd(sender: AnyObject) {
}
Run Code Online (Sandbox Code Playgroud)


Gur*_*ngh 5

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

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

//perform your action

}
Run Code Online (Sandbox Code Playgroud)


Esq*_*uth 5

Swift版本,在viewDidLoad中添加:

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

这在View Controller类中

func doneButton(sender: UIBarButtonItem) {
    println(111)
}
Run Code Online (Sandbox Code Playgroud)