Hai, saya perlu mengatur tombol di sisi kanan, di bilah navigasi, secara terprogram, sehingga jika saya menekan tombol saya akan melakukan beberapa tindakan. Saya telah membuat bilah navigasi, secara programatik oleh;
navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];
Demikian pula, saya perlu menambahkan tombol, di sisi kanan bilah navigasi ini. Untuk itu saya gunakan,
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];
Saya mencoba semua cara ini, tetapi tidak ada yang menampilkan tombol di sisi kanan.
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];
///// disebut acara
-(IBAction)Back_btn:(id)sender { //Your code here }
CEPAT:
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 }
sumber
Untuk menambahkan tombol pencarian pada bilah navigasi gunakan kode ini:
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(toggleSearch:)]; self.navigationController.navigationBar.topItem.rightBarButtonItem = searchButton;
dan terapkan metode berikut:
- (IBAction)toggleSearch:(id)sender { // do something or handle Search Button Action. }
sumber
Cara menambahkan tombol tambah ke navbar dengan cepat:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onAdd:")
onAdd:
sumber
self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)]autorelease]; -(void)saveAction:(UIBarButtonItem *)sender{ //perform your action }
sumber
Versi Swift, tambahkan ini di viewDidLoad:
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneButton:") navigationItem.rightBarButtonItem = doneButton
Dan ini di kelas View Controller Anda
func doneButton(sender: UIBarButtonItem) { println(111) }
sumber
Gunakan kode berikut:
UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStylePlain target:self action:@selector(customBtnPressed)]; [self.navigationItem setRightBarButtonItem:customBtn];
sumber
Sederhana menggunakan editBarButton asli seperti ini
self.navigationItem.rightBarButtonItem = self.editButtonItem; [self.navigationItem.rightBarButtonItem setAction:@selector(editBarBtnPressed)];
lalu
- (void)editBarBtnPressed { if ([infoTable isEditing]) { [self.editButtonItem setTitle:@"Edit"]; [infoTable setEditing:NO animated:YES]; } else { [self.editButtonItem setTitle:@"Done"]; [infoTable setEditing:YES animated:YES]; } }
Selamat bersenang-senang...!!!
sumber
Dalam metode ViewDidLoad dari ViewController.m
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(back)]; [self.navigationItem setLeftBarButtonItem:cancel];
Pemilih "(kembali)" adalah metode untuk menutup ViewController saat ini
sumber
Coba ini. Ini berhasil untuk saya. Tambahkan tombol ke bilah navigasi secara terprogram, Juga kami mengatur gambar ke tombol bilah navigasi,
Di bawah ini adalah Kode: -
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. }
sumber
Jika Anda tidak mencari BarButtonItem tetapi tombol sederhana di navigationBar, maka kode di bawah ini berfungsi:
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];
sumber
Halo semuanya !! Saya membuat solusi untuk masalah yang sedang dihadapi di mana Dua orientasi UIInterface diinginkan menggunakan UIIMagePicker .. Di ViewController tempat saya menangani segue ke UIImagePickerController
** Saya menggunakan ..
-(void) editButtonPressed:(id)sender { BOOL editPressed = YES; NSUserDefaults *boolDefaults = [NSUserDefaults standardUserDefaults]; [boolDefaults setBool:editPressed forKey:@"boolKey"]; [boolDefaults synchronize]; [self performSegueWithIdentifier:@"photoSegue" sender:nil]; }
**
Kemudian di AppDelegate Class I lakukan hal berikut.
- (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; }
sumber