2014-11-10 2 views
0

Я пытаюсь создать полностью настраиваемый UIToolbar, который содержит 2 полностью настроенных кнопки. Каждая ширина кнопки должна составлять 50% ширины панели инструментов.Настроить UIBarButtonItem с помощью uitoolbar

@property (weak, nonatomic) IBOutlet UIToolbar *ToolBar; 
-(void)viewDidLoad 
{ 
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom]; 

button.frame =CGRectMake(0, 0, self.ToolBar.frame.size.width/2-10,self.ToolBar.frame.size.height-2); 
button.layer.borderColor=[UIColor blueColor].CGColor; 
button.layer.borderWidth=1.0f; 
[button setTitle:@"Message" forState:UIControlStateNormal]; 
button.titleLabel.font=[UIFont boldSystemFontOfSize:14]; 
UIBarButtonItem *MsgButton =[[UIBarButtonItem alloc]initWithCustomView:button]; 
[MsgButton setTitle:@"Msg"]; 

UIButton *button2 =[UIButton buttonWithType:UIButtonTypeCustom]; 
button2.frame=CGRectMake(0, 0, self.ToolBar.frame.size.width/2-10,self.ToolBar.frame.size.height-2); 
button2.layer.borderColor=[UIColor redColor].CGColor; 
button2.layer.borderWidth=1.0f; 
[button2 setTitle:@"Message" forState:UIControlStateNormal]; 
button2.titleLabel.font=[UIFont boldSystemFontOfSize:14]; 
UIBarButtonItem *RelationButton =[[UIBarButtonItem alloc]initWithCustomView:button2]; 

[email protected][MsgButton,RelationButton]; 
} 

screen shot это скриншот того, как это выглядит на моем компьютере в настоящее время.

Вот проблемы:

  1. Как извлекал пространство слева и пространство между 2 кнопками
  2. Я попытался показать некоторый текст на кнопке не работал.
  3. Если возможно, могу ли я отображать только рамку слева или справа от кнопки?
+0

Проверьте это. Я управляю этим пространством в этом ответе - http://stackoverflow.com/questions/26475796/uibarbutton-item-on-screen-edge-in-ios-8-when-used-in-standalone-view/26479942#26479942 – Kampai

+0

Для вашего первого вопроса: вы должны изменить этот код в своем фрагменте, чтобы добиться желаемого изменения: 'self.ToolBar.frame.size.width/2-10' –

ответ

0

enter image description here Try This

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom]; 

    button.frame =CGRectMake(-15, 0, self.ToolBar.frame.size.width/2-20,self.ToolBar.frame.size.height-2); 
    button.layer.borderColor=[UIColor blueColor].CGColor; 
    button.layer.borderWidth=1.0f; 
    [button setTitle:@"Message" forState:UIControlStateNormal]; 
    button.titleLabel.font=[UIFont boldSystemFontOfSize:14]; 

    UIButton *button2 =[UIButton buttonWithType:UIButtonTypeCustom]; 
    button2.frame=CGRectMake(self.ToolBar.frame.size.width/2-35, 0, self.ToolBar.frame.size.width/2-20,self.ToolBar.frame.size.height-2); 
    button2.layer.borderColor=[UIColor redColor].CGColor; 
    button2.layer.borderWidth=1.0f; 
    [button2 setTitle:@"Message" forState:UIControlStateNormal]; 
    button2.titleLabel.font=[UIFont boldSystemFontOfSize:14]; 

    UIView *barView = [[UIView alloc] init]; 
    barView.frame = self.ToolBar.frame ; 
    [barView addSubview:button]; 
    [barView addSubview:button2]; 
    UIBarButtonItem *RelationButton =[[UIBarButtonItem alloc]initWithCustomView:barView]; 

    [email protected][RelationButton]; 
+0

спасибо за ответ. Тем не менее, я узнал, что для ширины рамки для buttton я устанавливаю его на self.toolbar.frame.size.width/2-5, чтобы соответствовать экрану apppart perferctly. В добавлении, все равно, что я могу только добавить границу справа или слева? –

Смежные вопросы