繁体   English   中英

使用uitoolbar自定义UIBarButtonItem

[英]Customize UIBarButtonItem with uitoolbar

我正在尝试创建一个完全自定义的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];
}

屏幕截图显示了它现在在我的PC上的屏幕截图。

这里是问题:

  1. 如何删除左侧的空间和2个按钮之间的空间
  2. 我试图在无法正常工作的按钮上显示一些文本。
  3. 如果可以,我只能在按钮的左侧或右侧显示边框吗?

在此处输入图片说明 尝试这个

 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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM