我正在尝试使用设置背景颜色和不透明度的 View 在图像顶部创建 mask ,它似乎在 iPhone 7 中运行良好。当我在 iPhone 中运行该应用程序时出现问题7 plus 模拟器,由于某种原因,面具保持 iPhone 7 尺寸的大小。
我创建了一个自定义的 UIImageView 类:
class LoginBackgroundImageView: UIImageView {
override func awakeFromNib() {
super.awakeFromNib()
// Set a mask on image background
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor(white: 0, alpha: 0.4)
bgColorView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
addSubview(bgColorView)
}
}
最佳答案
在将 bgColorView 添加为 subView 之后,尝试在 frame 的位置添加约束。
class LoginBackgroundImageView: UIImageView {
override func awakeFromNib() {
super.awakeFromNib()
self.clipsToBounds = true // set clipsToBounds true
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor(white: 0, alpha: 0.4)
addSubview(bgColorView)
bgColorView.translatesAutoresizingMaskIntoConstraints = false
bgColorView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
bgColorView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
bgColorView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
bgColorView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
}
希望这对您有所帮助。
关于ios - 在 Swift 3 中创建与 super View 相同大小的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42243269/