gpt4 book ai didi

ios - Swift 4 : UIView Subclassing, 获取父 View

转载 作者:行者123 更新时间:2023-11-30 11:20:56 26 4
gpt4 key购买 nike

我正在进行一个实践项目,试图实现这一目标。 enter image description here

使用 UIView 子类化 我快完成了,我和一些 SF 开发人员一起编写了这段代码。

 class Luna: UIView {

private var screenWidth: CGFloat {
return UIScreen.main.bounds.width
}

override init(frame: CGRect) {
super.init(frame: frame)

}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

}

public func showLuna(inView: UIView, title messageTitle:String, message messageDescription:String, dissmiss dissmissDuration: TimeInterval) {

let luna = UIView()

let titleLabel = UILabel()
titleLabel.text = "Introducing our new football notifications on the Guardian app"
titleLabel.font = UIFont(name: "avenirnext-demibold", size: 13)
//titleLabel.textColor = UIColor(hexString: "4A4A4A")
titleLabel.numberOfLines = 0 // whole idea of self-sizing

let titleDesc = UILabel()
titleDesc.text = "Today we are excited to announce the launch of our new and improved football notifications in our latest update to the Guardian app."
titleDesc.font = UIFont(name: "avenirnext-regular", size: 9)
//titleDesc.textColor = UIColor(hexString: "4A4A4A")
titleDesc.numberOfLines = 0

// My mainView

luna.backgroundColor = .white
//luna.addShadow(radius: 11, opacity: 0.2) // Some Shadow
luna.layer.cornerRadius = 10

let verticalStackView = UIStackView(arrangedSubviews: [titleLabel, titleDesc])
verticalStackView.axis = .vertical

let okButton = UIButton()
okButton.setTitleColor(.blue, for: .normal)
// okButton.setTitle("Okay", for: .normal)
okButton.setContentHuggingPriority(.defaultHigh, for: .horizontal) // to stretch the okay button horizontally

let horizontalStackView = UIStackView(arrangedSubviews: [verticalStackView, okButton])
horizontalStackView.axis = .horizontal

luna.addSubview(horizontalStackView)
self.addSubview(luna)

horizontalStackView.translatesAutoresizingMaskIntoConstraints = false // when using autolayout from code, this property must be false, otherwise constraint won't work
luna.translatesAutoresizingMaskIntoConstraints = false


NSLayoutConstraint.activate([
horizontalStackView.topAnchor.constraint(equalTo: luna.topAnchor, constant: 8),
horizontalStackView.bottomAnchor.constraint(equalTo: luna.bottomAnchor, constant: -8),
horizontalStackView.leadingAnchor.constraint(equalTo: luna.leadingAnchor, constant: 8),
horizontalStackView.trailingAnchor.constraint(equalTo: luna.trailingAnchor, constant: -8),

luna.topAnchor.constraint(equalTo: inView.safeAreaLayoutGuide.topAnchor, constant: 30),
luna.leadingAnchor.constraint(equalTo: inView.safeAreaLayoutGuide.leadingAnchor, constant: 30),
luna.trailingAnchor.constraint(equalTo: inView.safeAreaLayoutGuide.trailingAnchor, constant: -30)
])

}
}

但是,在我的另一个 ViewController 上调用 Show Luna 时,应用程序崩溃并给出以下内容:

2018-07-11 10:45:08.999314+0300 LunaView[1344:23604] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'

我猜测应用程序会由于限制而崩溃,因为要提供无效的 View 。将 Show Luna 代码粘贴到另一个 View Controller 的 ViewDidLoad 中是可行的,前提是将约束的 InView 更改为 self.view如:

SCR1

但我无法通过 View 子类化来做到这一点。甚至可以访问父级吗?

最佳答案

问题是您在 lunainView 之间创建了约束,而没有首先在其中插入 luna,因此在此行之后

 let luna = UIView()

添加

 inView.addSubview(luna)

关于ios - Swift 4 : UIView Subclassing, 获取父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51279988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com