ios - 不能在 UNNotificationContentExtension 上使用 AutoLayout 约束

标签 ios swift autolayout ios10 unusernotificationcenter

我正在尝试在 iOS 10 中添加自定义通知,该通知使用强制按下丰富的通知。我想以编程方式将 View 添加到继承自 UNNotificationContentExtension 的 View Controller 的中心,但我得到的只是一个白屏。当我给它一个像这样的框架时,内容被添加:

import UIKit
import UserNotifications
import UserNotificationsUI

class NotificationViewController: UIViewController, UNNotificationContentExtension {

    override func viewDidLoad() {
        super.viewDidLoad()
        let myView = UIView()
        myView.backgroundColor = UIColor.red
        myView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        self.view.addSubview(myView)
    }

    func didReceive(_ notification: UNNotification) {

    }

}

看起来像这样: enter image description here 但是当我尝试使用以下代码以编程方式(使用 PureLayout 包装器)使用 AutoLayout 时:

import UIKit
import UserNotifications
import UserNotificationsUI

class NotificationViewController: UIViewController, UNNotificationContentExtension {

    override func viewDidLoad() {
        super.viewDidLoad()

        let myView = UIView()
        myView.backgroundColor = UIColor.red
        self.view.addSubview(myView)

        myView.autoPinEdge(toSuperviewEdge: .top)
        myView.autoPinEdge(toSuperviewEdge: .left)
        myView.autoMatch(.width, to: .width, of: self.view)
        myView.autoMatch(.height, to: .height, of: self.view)
    }

    func didReceive(_ notification: UNNotification) {

    }

}

结果是这样的:enter image description here

什么会导致 AutoLayout 无法在此 View Controller 中工作?我也用界面生成器测试过它,所以我很困惑。

最佳答案

当您以编程方式创建 View 时,您必须记住将 translatesAutoresizingMaskIntoConstraints 设置为 false,否则自动布局将不会像您期望的那样运行。

关于ios - 不能在 UNNotificationContentExtension 上使用 AutoLayout 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40472293/

相关文章:

ios - usleep() 锁定iPhone提示

iphone - 有什么方法可以在不进入共享对话框的情况下在 google plus stream 上共享 ..?

ios - 不要将 Realm 对象映射到 Realm 表中

ios - UIView 使用约束自动布局居中和调整大小

ios - 受自动布局约束的动画 View

objective-c - 以编程方式打开位置服务 View

ios - Swift SDK 中会发生框架冲突吗?

swift - ios中如何让一个单元格容纳多个文本标签

Swift:使用协议(protocol)和子类进行动态调度

ios - 垂直对齐两个不同字体大小的标签