我想在UINavigationBar
上繪製多個按鈕。這些將在右側或左側。如何將幾個UIBarButtonItems添加到NavigationBar?
9
A
回答
10
我做了一個例子,在NaviagationBar的右側有兩個按鈕(即Edit和+)。 1)您必須創建一個NSMutableArray
(即示例中的「按鈕」),並在NSMutableArray
(即按鈕)中添加UIBarButtonItem
(即示例中的bi1和bi2)。
2)將NSMutableArray
(即實例中的按鈕)添加到工具欄(即在示例中爲UIToolbar *tools
)。
3)將工具欄添加到NavigationBar。
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add bar button1.
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
//[bi1 release]; Do not release if ARC enabled.
// Add bar button2.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
//[bi2 release]; Do not release if ARC enabled.
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
//[buttons release]; Do not release if ARC enabled.
// Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
//[twoButtons release]; Do not release if ARC enabled.
1
做在你的XIB文件,並進行性能或只是變量在頭
@property (nonatomic, retain) IBOutlet UIBarButtonItem *itemOne;
,然後在廈門國際銀行連接。享受
1
在代碼中創建一個新的UIToolbar並將您的按鈕添加到工具欄。然後設置self.navigationItem.rightBarButton到新創建的工具欄(注意例子是不ARC,所以你可能需要刪除呼叫釋放):
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addRow)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
[buttons addObject:self.editButtonItem];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
1
在這裏,我給你,我用按鈕的示例代碼以及標籤。你可以創建按鈕,而不是標籤和圖像我創建的。我希望它會幫助你
- (void) setLabelForPotraite {
bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor clearColor]];
barImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navImg.png"]];
[bar addSubview:barImg];
tick_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(86, 6,34, 33)];
tick_img_lbl.image=[UIImage imageNamed:@"tick-1.png"];
[bar addSubview:tick_img_lbl];
[tick_img_lbl release];
tickCount_lbl=[[UILabel alloc] initWithFrame:CGRectMake(126, 2, 50, 40)];
[email protected]"";
tickCount_lbl.font=[UIFont fontWithName:@"Arial" size:24.0];
[tickCount_lbl setTextAlignment:UITextAlignmentCenter];
tickCount_lbl.font = [UIFont boldSystemFontOfSize:24.0];
tickCount_lbl.textColor=[UIColor whiteColor];
tickCount_lbl.backgroundColor=[UIColor clearColor];
[bar addSubview:tickCount_lbl];
[tickCount_lbl release];
cross_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(181, 6, 34, 33)];
cross_img_lbl.image=[UIImage imageNamed:@"x_green.png"];
[bar addSubview:cross_img_lbl];
[cross_img_lbl release];
crossCount_lbl=[[UILabel alloc] initWithFrame:CGRectMake(221, 2, 50, 40)];
[email protected]"";
crossCount_lbl.font=[UIFont fontWithName:@"Arial" size:24.0];
crossCount_lbl.font = [UIFont boldSystemFontOfSize:24.0];
crossCount_lbl.textColor=[UIColor whiteColor];
[crossCount_lbl setTextAlignment:UITextAlignmentCenter];
crossCount_lbl.backgroundColor=[UIColor clearColor];
[bar addSubview:crossCount_lbl];
[crossCount_lbl release];
master_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(269, 6, 34, 33)];
master_img_lbl.image=[UIImage imageNamed:@"thumb.png"];
[bar addSubview:master_img_lbl];
[master_img_lbl release];
}
如果理解要不然那麼任何問題PLZ通知我。
相關問題
- 1. 如何將兩個UIBarButtonItems添加到UINavigationItem?
- 2. 添加BarButtonItem到NavigationBar
- 3. 如何將多個按鈕添加到NavigationBar?
- 4. 如何使用Interface Builder將UIBarButtonItems添加到UIViewController.toolbarItems屬性?
- 5. 將SearchBar添加到NavigationBar Objective-C iOS 9
- 6. 將幾何添加到THREE.Object3D
- 7. 如何將後退按鈕添加到NavigationBar?
- 8. 如何在使用UIPageViewController時將UIBarButtonItem添加到NavigationBar
- 9. 如何將NavigationBar(不帶NavigationController)和SearchBar添加到TableView中
- 10. 如何將左側的Editbutton的NavigationBar添加到Uitable In Iphone中?
- 11. 添加UIBarButtonItems到工具欄的問題
- 12. QLPreviewController刪除或添加UIBarButtonItems
- 13. 如何將幾個UIStepper值添加到一個標籤?
- 14. 將幾個組件添加到Jpanel中
- 15. 如何在導航欄的右側添加多個UIBarButtonItems?
- 16. 添加多個UIBarButtonItems到故事板中的UINavigationItem?
- 17. 向NavigationBar添加標題
- 18. 如何手動添加後退按鈕到我的NavigationBar?
- 19. 添加細分到幾何
- 20. 將UIImageView添加到NavigationBar的背面(不是背景圖片)
- 21. 將按鈕添加到NavigationBar的titleview而不必重複代碼
- 22. 以編程方式將NavigationBar添加到UITableViewController?
- 23. 如何將邊框添加到css中的幾何圖形?
- 24. 將兩個索引幾何體添加到BufferGeometry中
- 25. 如何添加幾個Checkboxs列
- 26. 如何在QGraphicsView中添加幾個QImage?
- 27. 如何動態添加幾個片段
- 28. Swift 3 - 添加BarButtonItem到NavigationBar在每個ViewController不重複代碼
- 29. 如何添加幾個.cpp文件到一個單一的CMakeLists.txt
- 30. 如何添加幾個月到一個特定的日期