How to hide the iOS 8 Today widget when not showing anything?

I use the Today widget for iOS8 to display information related to the current day. The problem is that I don’t want to display the widget / section at all if there are no corresponding messages to display.

I know that this should be possible, as the BA application does (it only shows the widget when there is a flight, but the rest of the time it is not visible at all). I just can't find a way to achieve this behavior.

Does anyone know how to do this?

+5
source share
4 answers

I found a way to do this using the NCWidgetController . This makes it easy to indicate when today's widget should be displayed based on any criteria that you consider necessary.

Just add the following to your viewDidLoad method (or anywhere that will be called when the widget restarts) in the modern widget manager, and it will work:

 BOOL hasDataToDisplay = NO; NCWidgetController *widgetController = [NCWidgetController widgetController]; [widgetController setHasContent:hasDataToDisplay forWidgetWithBundleIdentifier:@"com.my-company.my-app.my-widget"]; 

Apple Docs: https://developer.apple.com/library/ios/documentation/NotificationCenter/Reference/NCWidgetController_Class/index.html#//apple_ref/occ/cl/NCWidgetController

WARNING. NCWidgetController cannot be reset from the widget itself after you have determined that the content is not displayed. In other words, once you set it to NO , then there is no return unless you call it from the parent / containing application.

+12
source

In the ViewController widget's viewDidLoad method, add the following:

 BOOL DisplayWidget = NO; [[NCWidgetController widgetController] setHasContent:DisplayWidget forWidgetWithBundleIdentifier:@"<widget bunder identifier>"]; 

This will disable the display of the widget.

To enable it again, you must do this from the containing application using the same string passing YES for the setHasContent parameter. Be sure to add the necessary imports to the containing application in the ViewController, which will turn on the widget again:

 #import <NotificationCenter/NotificationCenter.h> @interface ViewController () <NCWidgetProviding> {...} 

[See page 41 of the documentation for widgets https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensibilityPG.pdf ]

+3
source

The approach that I used, although not ideal, has a small balance in the Notification Center, but it worked for me:

In viewDidLoad (), set the preferred content size size to 1:

 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view from its nib. preferredContentSize = CGSizeMake(0, 1) view.setNeedsLayout() } 

and then, when the widget updates, it gets the real height and sets it:

 var data: NSData? func updateData() { // fetch data if let data = data { let viewHeight: CGFloat // UI preperation and initialize viewHeight var preferredContentSize = CGSizeMake(0, viewHeight); } else { preferredContentSize = CGSizeMake(0, 1); } } func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) { // Perform any setup necessary in order to update the view. // If an error is encountered, use NCUpdateResult.Failed // If there no update required, use NCUpdateResult.NoData // If there an update, use NCUpdateResult.NewData updateData() completionHandler(data != nil ? NCUpdateResult.NewData : NCUpdateResult.NoData) } 
0
source

Better to use

 + (instancetype)widgetController 

then call

 - (void)setHasContent:(BOOL)flag forWidgetWithBundleIdentifier:(NSString *)bundleID 
-1
source

Source: https://habr.com/ru/post/1203503/


All Articles