我想创建一个view1,可以将它作为子视图添加到覆盖整个屏幕的视图控制器中。然后在view1内部,我想在中央放置一个100x100 view2。
如何快速完成此操作? (不是情节提要)。我想创建一个独立的uiview类,而不仅仅是在视图控制器中创建一个变量。
我要使view1 userInteractionDisabled。
答案 0 :(得分:1)
因为您想要独立的uiview类,所以要为两个视图创建两个类。这样可以更好地调制代码,如下所示:
class TypeOneView: UIView {
convenience init() {
self.init(frame: UIScreen.main.bounds)
isUserInteractionEnabled = false
// more ui customization
backgroundColor = UIColor.green
// add more customization if you want
}
}
class TypeTwoView: UIView {
convenience init(_ width: Double,_ height: Double) {
let fullScreenHeight = Double(UIScreen.main.bounds.height)
let fullScreenWidth = Double(UIScreen.main.bounds.width)
self.init(frame: CGRect(x: (fullScreenWidth/2) - (width/2), y: (fullScreenHeight/2) - (height/2), width: width, height: width))
// additional ui customization
backgroundColor = UIColor.red
// add more customization if you want
}
}
然后实例化这些视图并将其作为子视图添加到目标视图控制器类中,如下所示:
let view1 = TypeOneView()
let view2 = TypeTwoView(100.0, 100.0)
view.addSubview(view1)
view.addSubview(view2)
view.bringSubview(toFront: view2)
结果:
答案 1 :(得分:0)
var view1:UIView!
var view2:UIView!
override func viewDidLoad() {
view1.translatesAutoresizingMaskIntoConstraints = false
view1.isUserInteractionEnabled = false
self.view.addSubview(view1)
view1.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
view1.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
view1.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
view1.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
view2.translatesAutoresizingMaskIntoConstraints = false
self.view1.addSubview(view2)
view2.heightAnchor.constraint(equalToConstant: 100).isActive = true
view2.widthAnchor.constraint(equalToConstant: 100).isActive = true
view2.centerXAnchor.constraint(equalTo: self.view1.centerXAnchor).isActive = true
view2.centerYAnchor.constraint(equalTo: self.view1.centerYAnchor).isActive = true
}
答案 2 :(得分:0)
检查一下。就像这样
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
addMySubviews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addMySubviews()
}
func addMySubviews() {
let view = UIView(frame: CGRect.zero)
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
leadingAnchor.constraintEqualToSystemSpacingAfter(view.leadingAnchor, multiplier: 0).isActive = true
topAnchor.constraintEqualToSystemSpacingBelow(view.topAnchor, multiplier: 0).isActive = true
trailingAnchor.constraintEqualToSystemSpacingAfter(view.trailingAnchor, multiplier: 0).isActive = true
bottomAnchor.constraintEqualToSystemSpacingBelow(view.bottomAnchor, multiplier: 0).isActive = true
let view100x100 = UIView(frame: CGRect.zero)
view100x100.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(view100x100)
view100x100.centerXAnchor.constraintEqualToSystemSpacingAfter(centerXAnchor, multiplier: 0).isActive = true
view100x100.centerYAnchor.constraintEqualToSystemSpacingBelow(centerYAnchor, multiplier: 0).isActive = true
view100x100.heightAnchor.constraint(equalToConstant: 100).isActive = true
view100x100.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
}
按如下所述使用:
let myView = MyView(frame: CGRect(x: 0, y: 0, width: 275, height: 600))
OR
let myView = MyView(frame: view.frame)
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
myView.leadingAnchor.constraintEqualToSystemSpacingAfter(view.leadingAnchor, multiplier: 0).isActive = true
myView.topAnchor.constraintEqualToSystemSpacingBelow(view.topAnchor, multiplier: 0).isActive = true
myView.trailingAnchor.constraintEqualToSystemSpacingAfter(view.trailingAnchor, multiplier: 0).isActive = true
myView.bottomAnchor.constraintEqualToSystemSpacingBelow(view.bottomAnchor, multiplier: 0).isActive = true