簡體   English   中英

向工具欄添加按鈕

[英]Adding buttons to toolbar

我需要添加一個透明的工具欄,並添加4個帶有圖像的按鈕。 我該如何通過代碼做到這一點?

到目前為止我的工作;

我已經添加了工具欄並使其透明,現在我正嘗試添加4個帶有圖像的按鈕。 我怎樣才能做到這一點 ? (這4個按鈕也應具有action方法,因此當有人單擊該按鈕時,應觸發操作)

toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 320 , 320 , 60)];

[toolBar1 setBarStyle:UIBarStyleBlack];

[toolBar1 setTranslucent:YES];

NSMutableArray* allthebuttons = [[NSMutableArray alloc] initWithCapacity:4];

UIBarButtonItem *buttonWithImage = [[UIBarButtonItem alloc] ...... // Now what ??

[self.view addSubview:toolBar1];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(action];

UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"Item1" style:UIBarButtonItemStyleBordered target:self action:@selector(action];

NSArray *buttons = [NSArray arrayWithObjects: item1, item2, nil];
[toolBar setItems: buttons animated:NO];

[item1 release];
[item2 release];

只需為4個按鈕制作以上代碼

更新1:

使用以下代碼獲取按鈕中的圖像

UIImageView *btn1Img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Btn1Img"]];

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1Img];
NSMutableArray *buttonsArray = [[NSMutableArray alloc] init];
UIBarButtonItem *myButton1=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"email.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarButtonPressed1:)];
[buttonsArray addObject:myButton1];
UIBarButtonItem *myButton2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2" style:UIBarButtonItemStylePlain target:self action:@selector(toolbarButtonPressed2:)];  
[buttonsArray addObject:myButton2];
[self setToolbarItems:buttonsArray animated:YES];

請注意,對於上述內容,您將需要使用導航視圖控制器上的工具欄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM