Programmatically defining the height of a notification content extension
Asked Answered
G

3

6

The width:height ratio of a notification extension can be defined through attribute UNNotificationExtensionInitialContentSizeRatio in the extension's Info.plist file. But can this value somehow be changed after the respective UIViewController is loaded?

The documentation on Apple's site indicates that it is indeed possible:

You can change the size of your view controller after your extension loads.

But how? I've tried changing the extension's main UIView's frame, like so...

func didReceive(_ notification: UNNotification) {

    self.view.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: 100.0)
    self.view.setNeedsUpdateConstraints()
    self.view.setNeedsLayout()
}

...but only the view's frame is correctly updated, leaving the whole notification container with an empty white area at the bottom.

I've also attempted some less obvious solutions, including changing the frame at different points in the controller's lifecycle and setting the UIWindow's frame directly. None were successful so far.

Any idea if the height can be changed programmatically?

Thank you in advance for any answer.

Greerson answered 16/11, 2017 at 12:6 Comment(0)
N
11

You can override the size by using the preferredContentSize property of UIViewController.

Nottinghamshire answered 5/12, 2017 at 14:43 Comment(3)
Yep, that was it. Thanks!Greerson
It says Cannot override with a stored property 'preferredContentSize'Reneareneau
You need to set it somewhere in the view's lifecycle (e.g. viewDidLoad).Surrogate
U
4
 func didReceive(_ notification: UNNotification) {

    self.preferredContentSize = CGSize(width: UIScreen.main.bounds.size.width, height: 200)
    self.view.setNeedsUpdateConstraints()
    self.view.setNeedsLayout() 
  // Image downloading work or other logic
}
Undergird answered 10/3, 2019 at 11:9 Comment(0)
L
0

If you properly setup auto layout for all controls, the notification will resize itself correctly. There is no need to call any resize code.

Localize answered 20/2, 2020 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.