在导航栏中,我可以制作半透明的导航栏样式。但是我不能像那样在UIView中制作。 如何在UIView中制作半透明风格?
答案 0 :(得分:14)
在xCode项目中创建一个名为BlurView.swift的文件:
import UIKit
@IBDesignable class BlurView : UIView {
// Use the following property to set the tintColor. Set it to nil to reset.
@IBInspectable var blurTintColor: UIColor! {
set {
toolbar.barTintColor = blurTintColor
}
get {
return toolbar.barTintColor
}
}
lazy var toolbar:UIToolbar = {
// If we don't clip to bounds the toolbar draws a thin shadow on top
self.clipsToBounds = true
let toolbar = UIToolbar(frame: self.bounds)
toolbar.translatesAutoresizingMaskIntoConstraints = false
self.insertSubview(toolbar, atIndex: 0)
let views = ["toolbar": toolbar]
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[toolbar]|", options: [], metrics: nil, views: views))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[toolbar]|", options: [], metrics: nil, views: views))
return toolbar
}()
}
然后您可以通过编程或直接在IB中使用它。
使用与BlurView
相同的方式创建和使用UIVIew
。
var myView:BlurView // ...
// Activate the translucent effect by setting `blurTintColor`
myView.blurTintColor = UIColor.redColor() // Or any color
// If you need to desactivate the effect later
// myView.blurTintColor = nil
添加UIView并将其自定义视图设置为BlurView
:
然后您可以设置模糊色调以激活半透明效果:
注意:模糊仅在运行时出现。
答案 1 :(得分:11)
黑客会使用UIToolBar,就像你使用普通的UIView一样,因为在iOS 7中它会为你做模糊,比模糊效果自定义UIView的任何其他尝试都要好。 (这只适用于iOS7,iOS 6只需添加正常的alpha)
UIToolbar *toolBar = [[UIToolBar alloc] init];
[toolBar setFrame:kYourFrame];
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
[toolBar setAlpha:0.9];
}
[self.view addSubview:toolBar];
答案 2 :(得分:6)
您可以将UIToolBar中的模糊图层添加到您想要半透明的视图中。
看看这个开源项目:https://github.com/JagCesar/iOS-blur
答案 3 :(得分:2)
我推荐这个:https://github.com/ivoleko/ILTranslucentView 它提供原生iOS 7+模糊(半透明)效果