用UIToolBar实现简单毛玻璃效果

用UIToolBar实现简单毛玻璃效果

字数268 阅读1051 评论17 

UIToolBar自带毛玻璃效果,实现起来非常简单。

在View Controller中拖入一个UIImageView,并设置好图片。

接着我们准备在这个imageView上面加入一个UIToolBar,让这个toolBar变为这个imageView的子控件。

但是这个操作是不能在Storyboard中完成的。因为在Storyboard中除了UIView,其他的控件都不能直接在上面加上子控件。如果想给这个UIImageView上面加上子控件,那么需要用代码来实现。

Control-Drag设置这个UIImageView的IBOutlet,名为imageView。并且创建一个UIToolBar名为toolBar。接着给imageView发送addSubview:消息,将toolBar加到imageView上。代码如下:

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:self.imageView.bounds]; // toolBar的frame就是imageView的bounds。

    [self.imageView addSubview:toolBar];
}

@end

效果如下:

另外,我们可以通过修改toolBar的barStyle属性来实现不同风格的毛玻璃效果:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:self.imageView.bounds];
    toolBar.barStyle = UIBarStyleBlack; // 改变barStyle
    [self.imageView addSubview:toolBar];
}
原文地址:https://www.cnblogs.com/leiming1001/p/5273030.html