简体   繁体   中英

Can't use AutoLayout Constraints on UNNotificationContentExtension

I'm trying to add a custom notification in iOS 10 that utilizes the force press rich notifications. I want to programmatically add a view to the center of the view controller that inherits from UNNotificationContentExtension, but all I get is a white screen. The content is added when I give it a frame like this:

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) {

    }

}

Which looks like this: 在此处输入图片说明 But when I try using AutoLayout programmatically (with the PureLayout wrapper) using this code:

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) {

    }

}

The result is this: 在此处输入图片说明

What would cause AutoLayout to not work in this view controller? I've tested it with the interface builder as well, so I'm quite confused.

以编程方式查看视图时,必须记住将translatesAutoresizingMaskIntoConstraints设置为false,否则自动布局的行为将不符合您的预期。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM