gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:15 25 4
gpt4 key购买 nike

我正在尝试在 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/

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