Tôi muốn vẽ nhiều nút trên UINavigationBar
. Chúng sẽ ở bên phải hoặc bên trái.Làm thế nào để thêm một số UIBarButtonItems vào một NavigationBar?
Trả lời
Tôi đã làm một ví dụ trong đó tôi có hai nút (tức là Chỉnh sửa và +) ở bên phải của NaviagationBar.
1) Bạn phải tạo một NSMutableArray
(ví dụ: "nút") và thêm UIBarButtonItem
(ví dụ: bi1 và bi2) vào NSMutableArray
(tức là các nút).
2) Thêm NSMutableArray
(ví dụ: các nút chẳng hạn) vào thanh công cụ (ví dụ: UIToolbar *tools
chẳng hạn).
3) Thêm thanh công cụ vào 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.
làm điều đó trong file xib của bạn và làm cho tài sản hoặc chỉ biến trong tiêu đề
@property (nonatomic, retain) IBOutlet UIBarButtonItem *itemOne;
và sau đó kết nối nó trong xib. Thưởng thức
Tạo thanh UIToolbar mới trong mã và thêm các nút của bạn vào thanh công cụ. Sau đó thiết lập self.navigationItem.rightBarButton vào thanh công cụ vừa tạo (lưu ý ví dụ này là không ARC, do đó bạn có thể cần phải loại bỏ các cuộc gọi để giải phóng):
// 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];
Ở đây tôi đang đem lại cho bạn mẫu mã mà tôi sử dụng cho Button cũng như nhãn. bạn có thể tạo nút thay vì nhãn và hình ảnh những gì tôi đã tạo. Tôi hy vọng nó sẽ giúp bạn
- (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];
}
Nếu bất kỳ vấn đề trong việc tìm hiểu hoặc nếu không thì plz thông báo cho tôi.
- 1. Làm cách nào để thêm hai UIBarButtonItems vào UINavigationItem?
- 2. Làm thế nào để thêm NavigationBar (không có NavigationController) và SearchBar vào một TableView
- 3. Android, làm thế nào để thêm một số đệm phải vào văn bản trong một spinner?
- 4. Làm thế nào để đúc một số vào một byte?
- 5. Làm thế nào để đặt một hình ảnh ở trung tâm của navigationBar của một UIViewController?
- 6. Làm thế nào để thêm đệm vào một nhãn UILabel?
- 7. Làm thế nào để thêm jstl vào một portlet Liferay?
- 8. Làm thế nào để thêm hàng vào giữa một TableLayoutPanel
- 9. Thêm các mục vào NavigationBar (Không sử dụng UINavigationController)
- 10. Tự động xem tiêu đề trong một NavigationBar với autolayout
- 11. Làm uibarbuttonitems mờ dần
- 12. Làm thế nào để thêm một giá trị số nguyên vào NSMutableArray?
- 13. Làm thế nào để thêm số giờ thay đổi vào một ngày trong PostgreSQL?
- 14. Làm thế nào để nhập một đối số vào CGContextSetRGBFillColor?
- 15. Làm cách nào để thêm số không được đệm vào một số trong Java?
- 16. Làm thế nào để thêm một lớp vào một hàng mới trong một datatables jquery?
- 17. Làm thế nào để thêm một cột từ một tập tin vào một tập tin
- 18. Làm thế nào để thêm một StringFormat vào một Textblock bên trong một DataTemplate?
- 19. Làm thế nào để thêm một nút vào một khung làm việc nhỏ gọn DataGrid?
- 20. Làm thế nào để Thêm Một SyndicationElementExtension Để Một SyndicationItem
- 21. Làm thế nào để Thêm một Tooltip Để một DataGridTextColumn
- 22. Làm cách nào để thêm một nghìn seperator vào một số trong JavaScript?
- 23. Python: làm thế nào để thêm nội dung của một iterable vào một bộ?
- 24. Center một nút trong một Navigationbar
- 25. Làm thế nào để nối thêm một hàng vào một TableViewSection trong Titanium?
- 26. Làm thế nào để thêm các mục vào một stringArray trong một arrayList theo lập trình?
- 27. làm thế nào để một mảnh vỡ được thêm vào trong một layout XML
- 28. Làm thế nào để thêm một Clojure REPL vào một ứng dụng .NET hiện có?
- 29. Làm thế nào để thêm một nút radio vào một biểu mẫu với Zend Framework?
- 30. Làm thế nào để thêm một GUID simpleType vào một lược đồ XML?