【发布时间】:2013-12-03 03:46:24
【问题描述】:
在导航栏中,我可以制作半透明的导航栏样式。但我不能像这样在 UIView 中制作。 如何在 UIView 中制作半透明样式?
【问题讨论】:
标签: ios uiview ios7 uinavigationbar
在导航栏中,我可以制作半透明的导航栏样式。但我不能像这样在 UIView 中制作。 如何在 UIView 中制作半透明样式?
【问题讨论】:
标签: ios uiview ios7 uinavigationbar
在您的 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
##With 界面生成器
添加一个 UIView 并将其自定义视图设置为 BlurView :
然后你可以设置 Blur Tint Color 来激活半透明效果:
注意:模糊只会在运行时出现。
##结果
【讨论】:
ClipToBounds = true 分配给它。但是感谢这个解决方案,这是关键!
我推荐这个:https://github.com/ivoleko/ILTranslucentView 它提供原生 iOS 7+ 模糊(半透明)效果
【讨论】:
黑客会像使用普通 UIView 一样使用 UIToolBar,因为在 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];
【讨论】:
您可以从 UIToolBar 添加一个模糊层到您希望它是半透明的视图。
看看这个开源项目:https://github.com/JagCesar/iOS-blur
【讨论】: