前言
UIToolbar以前也接触过,不过没有怎么用,久而久之就忘了他的存在,今天看别人源码的时候看见了,它怎么很方便,在排列一排视图的时候不需要我们算里面的坐标就可以轻松良好的把布局做出来
代码
UIToolbar *tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, 300, 50)]; tool.layer.borderWidth = 1; tool.barStyle = UIBarStyleBlack; tool.backgroundColor = [UIColor blackColor]; [self.view addSubview:tool]; //中间空余部分 UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]; UIBarButtonItem *previousItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"player_pause"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(previous:)]; UIBarButtonItem *previousItem1 = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"player_pause"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(previous:)]; UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 0, 150, 20)]; UIBarButtonItem *volumnItem = [[UIBarButtonItem alloc]initWithCustomView:slider]; tool.items = @[space,previousItem1,space,volumnItem,space,previousItem];
效果图