2015-10-07 118 views
0

我添加了一個工具欄,然後以編程方式向其添加3個按鈕。然而,3個按鈕都混亂到一個角落。我想要做的是在3個按鈕之間留出空間,以便UI看起來很體面。以編程方式將按鈕添加到UIToolBar

我該怎麼做?

UIButton *button = [UIButton buttonWithType:100]; 

[button addTarget:self action:@selector(hideGender:) forControlEvents:UIControlEventTouchUpInside]; 

[button setImage:[UIImage imageNamed:@"img"] forState:UIControlStateNormal]; 

[button setTitle:@"Images" forState:UIControlStateNormal]; 

button.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0, 50); 

button.tag=0; 





UIButton *button2 = [UIButton buttonWithType:100]; 

[button2 addTarget:self action:@selector(hideSchool:) forControlEvents:UIControlEventTouchUpInside]; 

button2.tag=2; 

button2.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0, 50); 

[button2 setImage:[UIImage imageNamed:@"img22"] forState:UIControlStateNormal]; 



UIButton *button3 = [UIButton buttonWithType:100]; 

[button3 addTarget:self action:@selector(hideCar:) forControlEvents:UIControlEventTouchUpInside]; 

button.tag=3; 

button3.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0,50); 

[button3 setImage:[UIImage imageNamed:@"img3"] forState:UIControlStateNormal]; 



UIBarButtonItem *barButtonItem0 = [[UIBarButtonItem alloc] initWithCustomView:button] ; 

UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2] ; 

UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithCustomView:button3] ; 

更新:我希望只使用UIButton的

回答

0
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btn setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal]; 
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithCustomView:btn]; 
[toolBar setItems:[NSArray arrayWithObject:barBackButton]]; 

現在setItems設定按鈕的陣列。假設你想放三個按鈕,那麼每個按鈕的寬度將是screenWidth/3,文本將居中對齊。這樣,沒有按鍵的超越。

+0

我想使用UIButtons。 – Illep

+0

好的,讓我編輯我的答案... – pkc456

相關問題