Change NCWidgetDisplayMode programmatically in IOS10 Widget
In iOS 10
, Show More/Show Less
button is automatically provided in the Today's Extension
. So height of the widget
is handled automatically through NCWidgetDisplayMode
. You don't need to provide any explicit button for handling the widget's height.
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOSApplicationExtension 10.0, *) {
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
}
}
Implement NCWidgetProviding
protocol's method:
@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if activeDisplayMode == .expanded {
preferredContentSize = CGSize(width: maxSize.width, height: 300)
} else {
preferredContentSize = maxSize
}
}
In, iOS 8
and iOS 9
, you need to explicitly handle widget's height. In iOS 10
, it is not required.
You can refer to https://github.com/pgpt10/Today-Widget on Today's Widget
implementation in iOS 8
, iOS 9
and iOS 10
.
Today Widget Extension Height - iOS10
The height of the widget in iOS 10 is exactly 110 in compact mode. It can be set to whatever height you want in expanded mode, but in compact mode it will always be 110 and that can't be overwritten.
Swift - iOS10 - CoreData - FetchRequest in Today Widget always return 0 elements
You have defined a PersistentContainer
subclass to override defaultDirectoryURL
, but your CoreDataStack
is still using the build in NSPersistentContainer
type for your lazy container.
This should do the trick:
public class CoreDataStack {
public static let shared = CoreDataStack()
public var errorHandler: (Error) -> Void = {_ in }
//#1
public lazy var persistentContainer: PersistentContainer = {
let container = PersistentContainer(name: CoreDataServiceConsts.modelName)
container.loadPersistentStores(completionHandler: { [weak self](storeDescription, error) in
if let error = error {
NSLog("CoreData error \(error), \(error._userInfo)")
self?.errorHandler(error)
}
})
return container
}()
Cheers
Michał
How to create widgets showing on home screen in iOS 10
Swift3 & Xcode8
For above you need to create an extension for your Main App
Which extension I need to create to achieve above one?
Ans:Today Extension
How to create Extension?
Xcode -> File -> New -> Target ->Today Extension
After Creating Extension, If you want to pass data from Main App to Extension then you need know about AppGroups
AppGroups is nothing but to share data between Main App to Extensions
How to achieve?
Just Simple /p>
Go to Xcode -> Capabilities -> AppGroups Enable -> Click + -> Add a New Container with format group.*
for Example: group.com.yourCompany.ProjectName
Go to Main App
Initialise defaults with Suite
var appGroupDefaults = UserDefaults.standard
appGroupDefaults = UserDefaults(suiteName:"group.com.yourCompany.ProjectName")!
After initialising defaults with Suite and Set the ArrayData Or String Data whatever you need to pass to Extension
appGroupDefaults.set(value: arrayDataToPasstoTodayExtension, forKey: "arrayDatatoDisplayInToday")
Then after Retrieve data in Extension /p>
var appGroupDefaults = UserDefaults.standard
appGroupDefaults = UserDefaults(suiteName:"group.com.yourCompany.ProjectName")!
let dataArray = appGroupDefaults.value(forKey: "arrayDatatoDisplayInToday")! as! NSMUtableArray
Print("Hurray i got the data from Main App to Extension")
In Extension
For ShowLess and ShowMore more Options in Widget[Today]
override func viewDidLoad() {
self.preferredContentSize = CGSize(width: 320, height: CGFloat(yourArrayValuesCount.count)*90 )
if #available(iOSApplicationExtension 10.0, *) {
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
} else {
// Fallback on earlier versions
}
}
// For iOS 10
@available(iOS 10.0, *)
@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
self.preferredContentSize = (activeDisplayMode == .expanded) ? CGSize(width: 320, height: CGFloat(yourArrayValuesCount.count)*90 ) : CGSize(width: maxSize.width, height: 90)
}
Related Topics
Swift Socket.Io .Emit() Doesn't Fire
Uitapgesturerecognizer Sender Is the Gesture, Not the UI Object
Playing an Audio File Repeatedly with Avaudioengine
Infinite Scroll on iOS with Swift
How to Create Gmsmarker with Combined Images in Swift
Pausing Spritekit Game Using Appdelegate in iOS8
Detect When Uitableviewcell Did Load
How to Use Nsuserdefaults to Store an Array of Custom Classes in Swift
Resize Inputaccessoryview Dynamically in iOS 8
How to Fill a Circle Color by Percentage Value
How to Draw Geojson in Apple Maps as Overlay Using Swift 3
Simplest Way to Implement a "Read More" Button to Expand a Uitextview in iOS Swift 2
How to Delete an Image from Photolibrary After I Pick It Up Using Uiimagepickercontroller