Tambahkan tombol ke bilah navigasi secara terprogram


89

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.

Jawaban:


187

Di dalam UIViewControllerkelas turunan saya , saya menggunakan yang berikut di dalam viewDidLoad:

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

Ini menambahkan tombol ke sisi kanan dengan judul Flip, yang memanggil metode:

-(IBAction)flipView

Ini terlihat sangat mirip dengan Anda # 3, tetapi berfungsi dalam kode saya.


Saya perlu menambahkan titik dua ke selektor - "action: @selector (flipView :)];
Rydell

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

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

10

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

terima kasih banyak atas solusinya. Barang adalah hal yang saya lewatkan
Kunal Gupta

6

Cara menambahkan tombol tambah ke navbar dengan cepat:

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

onAdd:

func onAdd(sender: AnyObject) {
}

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

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

//perform your action

}

5

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

4

Gunakan kode berikut:

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

3

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


3

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


3

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

1

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

-1

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

Maaf apa pertanyaannya !! : P
Shad
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.