- UIToolbar是什么
UIToolbar继承自UIView
要创建工具栏项,请使用UIBarButtonItem类。要将工具栏项添加到工具栏中,可以使用setItems:animated:方法。
表示项目的正常状态和突出显示状态的工具栏图像来自您使用从UIBarItem类继承的image属性设置的图像。图像使用工具栏的tintColor着色。
如果您需要单选按钮样式控件,请使用UITabBar类而不是UIToolbar。 - UIToolbar的创建
1.使用xib
2.通过代码创建
[[UIToolbar alloc]init];
-
UIToolbar设置背景颜色
1.虽然UIToolbar继承自UIView,理论上可以通过backgroundColor设置背景颜色,但是设置的结果却不尽人意:
WeChat35394d4b35d2713b1c3b53c206b56fb9.png
所以设置UIToolbar对象的背景颜色通过barTintColor设置是最好的
-
UIToolbar的默认效果如图:
WechatIMG61.png
1.可以通过setShadowImage来达到隐藏横线的效果
[self.oneToolBar setShadowImage:[UIImage new] forToolbarPosition:UIBarPositionAny];
- UIToolbar添加item
1.通过UIBarButtonItem来设置UIToolbar上面的item,但是通过实践得知我们无法自定义通过修改item的宽度来实现自定义布局 但是我们可以通过UIBarButtonItem的UIBarButtonSystemItem属性中的UIBarButtonSystemItemFlexibleSpace来实现填充布局
默认情况系下设置item的,UIToolbar的布局会按照从左到右等间距布局 样式如下:
- (void)setItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated;
UIBarButtonItem*item7=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:@selector(item1Action)];

网友评论