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

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

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

同样,我需要添加button,在这个导航栏的右侧。 对于我使用的,

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

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

在我的UIViewController派生类里面,我在viewDidLoad里面使用了下面的代码:

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

在右边添加一个buttonFlip,它调用方法:

-(IBAction)flipView

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

 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 } 

要在导航栏上添加searchbutton,请使用以下代码:

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

如何在swift中添加一个添加button到导航栏中:

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

使用onAdd:

 func onAdd(sender: AnyObject) { } 

Swift版本,将其添加到viewDidLoad中:

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

这在你的视图控制器类

 func doneButton(sender: UIBarButtonItem) { println(111) } 
 self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)]autorelease]; -(void)saveAction:(UIBarButtonItem *)sender{ //perform your action } 

在ViewController.m的ViewDidLoad方法中

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

“(后退)”select器是解散当前ViewController的一种方法

简单的使用原生的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]; } } 

玩的开心…!!!

试试这个,它为我工作。 添加button导航栏编程,我们也设置图像导航栏button,

以下是代码: –

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

使用以下代码:

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

如果你不是在寻找一个BarButtonItem,而是在navigationBar上的简单button,那么下面的代码工作:

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

大家好 !! 我使用UIIMagePicker创build了需要两个UIInterface方向的问题的解决scheme。在我的ViewController中,我将Seque处理为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; }