添加全屏视图作为视图控制器的子视图
Posted
技术标签:
【中文标题】添加全屏视图作为视图控制器的子视图【英文标题】:Add fullscreen view as subview of view controller 【发布时间】:2018-08-28 04:40:23 【问题描述】:我想制作一个可以作为子视图添加到覆盖整个屏幕的视图控制器的 view1。然后在 view1 内,我想在中心放置一个 100x100 view2。
我怎样才能用 swift 做到这一点? (不是故事板)。我想创建一个独立的 uiview 类,而不仅仅是视图控制器中的一个变量。
我想让 view1 userInteractionDisabled。
【问题讨论】:
【参考方案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)
结果:
【讨论】:
【参考方案2】: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
【讨论】:
【参考方案3】:看看这个。应该是这样的
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))
或
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
【讨论】:
以上是关于添加全屏视图作为视图控制器的子视图的主要内容,如果未能解决你的问题,请参考以下文章