我將代碼添加到ViewController的viewDidLoad方法中。我有兩個UIBarButtonItem,其中一個在左邊叫做Left,另一個在右邊叫Right。我可以讓左邊的按鈕工作,但我甚至看不到正確的按鈕。我究竟做錯了什麼?如何以編程方式添加兩個UIBarButtonItem的UINavigationBar?
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 420, 44)];
[navbar setBackgroundImage:[UIImage imageNamed:@"bg_fade.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"Tray Tracker"];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self action:
@selector(leftAction:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:
@selector(saveAction:)];
item.leftBarButtonItem = leftButton;
item.rightBarButtonItem = rightButton;
navbar.items = @[item];
[self.view addSubview:navbar];
我確實有leftAction和saveAction
swcreenshot http://imageshack.com/a/img560/5706/skve.png
我以前使用過UIToolbar,它工作的很棒。我將研究UINavigationController。 – Pramesh
查看我的編輯。我認爲你的問題僅僅是酒吧框架 – adamF
這是做到了。接得好 – Pramesh