UNNotificationContentExtension不能使用AutoLayout约束
问题描述:
我试图在iOS 10中添加一个自定义通知,该通知利用了强制按下丰富的通知。我想以编程方式将视图添加到从UNNotificationContentExtension继承的视图控制器的中心,但我得到的只是一个白色屏幕。UNNotificationContentExtension不能使用AutoLayout约束
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) {
}
}
,看起来像这样:当我给它这样一个框架的内容添加 但是当我尝试使用此代码使用自动版式编程(与PureLayout包装):
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) {
}
}
什么会导致AutoLayout无法在此视图控制器中工作?我也用界面生成器测试了它,所以我很困惑。
答
当您以编程方式制作视图时,必须记得将translatesAutoresizingMaskIntoConstraints
设置为false或自动布局的行为不像您期望的那样。
这绝对是我错过的东西,但它仍然没有解决问题。如果我将self.view的背景颜色设置为黑色,背景会达到我的预期。只要我添加一个约束到myView我得到一个空白的屏幕。 – tdon