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

标签 ios swift swift4

我正在进行一个实践项目,试图实现这一目标。 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/

相关文章:

ios - 通用链接适用于 xcode8 模拟器,但不适用于 ios 10 设备

c++ - 为 arm64 iPhone 编译 xcode 5 失败

swift - 实现 Equatable Void (None) 类型

macos - OSX Swift 项目中的 FMDB -> 许多错误

swift 4 firebase GeoPoint 解码

swift4 - 错误: could not find target(s): MyLib; use the 'path' property in the Swift 4 manifest to set a custom target path

ios - Apple 文件应用程序通过文件提供程序扩展执行 "Move"操作我的扩展显示禁用

ios - 需要更改 NSURLConnection 方法以通过身份验证质询

ios - iPhone 5 和 iPhone 6 中的不同 Int 类型

javascript - 转换 UIWebView 中的触摸点以保存内容