我正在尝试创建一个完全自定义的UIToolbar,其中包含2个完全定制的按钮。每个按钮的宽度应该是工具栏宽度的50%。
@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];
self.ToolBar.items=@[MsgButton,RelationButton];
}屏幕截图这是一张屏幕截图,显示它现在在我的电脑上的样子。
以下是问题所在:
发布于 2014-11-10 06:35:32

尝尝这个
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];
self.ToolBar.items=@[RelationButton];https://stackoverflow.com/questions/26837477
复制相似问题