Maximum height of iOS 8 Today Extension? - ios

I am working on a Today Extension with a dynamically sized table. I have been able to get the table to resize for the content using:
self.preferredContentSize = accountsTable.contentSize
However, I have found that it will not get taller than a certain size (568 px) even though I can tell the table contentSize is larger.
I'm not clear if this is a built-in limit or if there is a way around this to make a larger view. It appears that some previous extensions (Stocks widget) is able to become larger.
Anyone else running into the same behavior. Anyone know if it's possible to make an extension appear larger either immediately or using a "Show All" button like the Stock widget?

I made some tests and you can calculate the maximum height of your Today Extension with this formular:
for iPhone:
float maxHeight = [[ UIScreen mainScreen ] bounds ].size.height - 126;
for iPad:
float maxHeight = [[ UIScreen mainScreen ] bounds ].size.height - 171;
This should work for alle Screen sizes...

Ok, although you can assign to the preferredContentSize any CGSize it is reduced to max height and width.
Device Orientation (max width, max height):
iPhone 5S Portrait (272, 441.5)
iPhone 5S Landscape (520, 205.5)
iPhone 6 Portrait (327, 540.5)
iPhone 6 Landscape (586, 260.5)
iPhone 6 Plus Portrait (362, 610)
iPhone 6 Plus Landscape (585, 288)
iPad Mini Portrait (535, 853)
iPad Mini Landscape (535, 597)
iPad Portrait (711, 985)
iPad Landscape (967, 729)
How did I get these values?
#IBOutlet weak var sizerView: UIView!
inside viewDidLoad:
preferredContentSize = CGSizeMake(0, 2000)
dispatch_after(1, dispatch_get_main_queue(), { //it needs time to render itself
label.text = NSStringFromCGSize(self.sizerView.bounds.size) //here you have MAX VALUES
}

It seems, that iOS is adding an UIView-Encapsulated-Layout-Height constraint to your own constraints:
(
"<NSLayoutConstraint:0x610000280640 V:[_UILayoutGuide:0x7f92d0513810]-(500)-[UILabel:0x7f92d040cb20'Hello World']>",
"<NSLayoutConstraint:0x610000280690 V:[UILabel:0x7f92d040cb20'Hello World']-(500)-[_UILayoutGuide:0x7f92d0515100]>",
"<_UILayoutSupportConstraint:0x6100002a2f40 V:[_UILayoutGuide:0x7f92d0513810(0)]>",
"<_UILayoutSupportConstraint:0x6100002a2ee0 V:|-(0)-[_UILayoutGuide:0x7f92d0513810] (Names: '|':UIView:0x7f92d040c810 )>",
"<_UILayoutSupportConstraint:0x6100002a3000 V:[_UILayoutGuide:0x7f92d0515100(0)]>",
"<_UILayoutSupportConstraint:0x6100002a2fa0 _UILayoutGuide:0x7f92d0515100.bottom == UIView:0x7f92d040c810.bottom>",
"<NSLayoutConstraint:0x60800009e780 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7f92d040c810(628)]>"
)
This constraint forces the widget to have the maximum height. You can get it's value like this:
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
for (NSLayoutConstraint *constraint in self.view.constraints) {
if ([constraint.identifier isEqualToString:#"UIView-Encapsulated-Layout-Height"]) {
NSLog(#"Height: %#", #(constraint.constant));
}
}
completionHandler(NCUpdateResultNewData);
}

To get the max height of the widgets active display mode, do this in your UIViewController:
let context = self.extensionContext
if let context = context{
let height = context.widgetMaximumSize(for: context.widgetActiveDisplayMode).height
}
To get the max height of expanded and compact individually regardless of the current display mode, do this:
var context = self.extensionContext
if let context = context{
let compactHeight = context.widgetMaximumSize(for: .compact).height
let expandedHeight = context.widgetMaximumSize(for: .expanded).height
}

The recommended way is to use auto-layout constraints to constrain your view's height.
It seems the 568px is just the maxheight of your iPhone 5 and not the actual size. As far as I could figure out there is no way to get the real size.

Related

Screen hight with (UIScreen) is fine in iPad but wrong in iPhone

I am developing a game where a background is moving at the back. I sat the background hight using the below code. It is working fine with iPads but with iPhones it is not taking the proper hight of the screen; instead it is in the middle of the screen with almost half the hight of the screen! The application is only working in portrait orientation. This is for xCode 8 & SWIFT 3
Please check below the code I am using for the background
hight:
let screenSize: CGRect = UIScreen.main.fixedCoordinateSpace.bounds
background1.size.height = screenSize.height
Thanks in advance
I have resolved the issue but using a (workaround) rather than resolving the root cause as I could not identify it.
First I have noticed that when sizing the hight of the screen I have 2 conditions:
below code will work with all iPhones but not iPads:
background1.size.height = self.frame.height
below code will work with all iPads but not iPhones:
background1.size.height = screenSize.height
So since the minimum hight of screen of iPads is 1024; I used the below code to first check if it is iPad use condition 2 and if iPhone use condition 1 as follow:
let screenSize: CGRect = UIScreen.main.bounds
if screenSize.height < CGFloat(1024) {
background1.size.height = self.frame.height ////for iphone
} else {
background1.size.height = screenSize.height ////for ipad
}
I am not sure what your case is.But it happens in iPhone 5+ devices due to launch screen size.Normally it behaves like iPhone 5 screen size even with iPhone 6,iPhone 6+.
Now the soultion.
1.You can use the default launch screen storyboard to avoid the problem.
2.You have to add every sizes for iPhone screen size (320*480,640*960,640*1136,768*1334,1242*2208).please add also iPhone 7,7+ sizes too.
3.You can use asset Catalogs for launch screen.
If thats the case for you please use above solution.It should work.

Resize a UILabel so it looks the same on iPhone and iPad

I am trying to adapt an existing iPhone only app to support the iPhone 6's as well as iPads. I do not want the app layout to change I just want it to resize to fit the various devices.
A number of UILabels on screen have updatable content which is truncated with an ellipsis if too long.
I would love to be able to do this with autolayout in a storyboard but if code is the only way to do it then so be it.
Any suggestions would be greatly appreciated.
You can programmatically set a font size as a fraction of screen size. Like so:
/*
* Screen size is orientation dependent on iOS 8, so take the shortest
* dimension as a rough measure of screen size
*/
CGSize size = [UIScreen mainScreen].bounds.size;
CGFloat sizeFactor = (size.width < size.height) ? size.width : size.height;
label.font = [UIFont systemFontOfSize:sizeFactor / 16.f];

Trying to detect iPhone 6 Plus in landscape orientation

I have a today widget for my app and I have been setting the layout of objects in code (Not using auto-layout) however, I need to detect when a user is using an iPhone 6 Plus. The reason for this is because in notification centre on the 6 Plus, there is a border around the edge which cuts off the labels in my widget (in landscape).
The problem is that I have been having issues trying to figure out what height value the screen is in landscape.
This is the code I have right now:
var height = UIScreen.mainScreen().bounds.size.height
if UIDevice.currentDevice().orientation.isLandscape && height == 414 {
totalBudgetDisplay.frame.origin.x = UIScreen.mainScreen().bounds.width - 250
currentBudgetDisplay.frame.origin.x = UIScreen.mainScreen().bounds.width - 250
budgetNameDisplay.frame.origin.x = 80
warningLabel.center = view.center
button.frame = CGRectMake(0, 0, view.bounds.width, view.bounds.height)
}
Is this the correct value for the screen height in landscape? If so, why are the labels still not moving?
Your code is good but UIDevice.currentDevice().orientation.isLandscape just does not get you correct value.
Seems like there is no bullet-proof solution to detect device orientation for Extensions.
Look at this answer for possible workaround - https://stackoverflow.com/a/26023538/2797141

xcode 6 is iphone 5 no longer 568 points in height?

So I'm trying to update my game to fit the iPhone 6 properly and i see now that when i run my game on the iPhone 5 simulator, everything is fitting as if it were an iPhone 4. I use if statements to determine the height of the device:
if ((int)[[UIScreen mainScreen] bounds].size.height == 568)
{
mylabel4.fontSize = 35;
mylabel4.position = CGPointMake(CGRectGetMidX(self.frame) -145, CGRectGetMidY(self.frame) -130);
} else {
mylabel4.fontSize = 35;
mylabel4.position = CGPointMake(CGRectGetMidX(self.frame) -110, CGRectGetMidY(self.frame) - 130);
}
And like i said when i run the iPhone 5 simulator, it runs the code in the 'else' bracket. Its like the iPhone 5 is no longer 568 points in height. This was working fine until i download Xcode 6? What's going on here?
The UIScreen bounds behavior has changed in iOS8. Previously it would always give you width and height of the screen in portrait (so 320 x 568 on the iPhone 5), but in iOS8 it will give you width and height with respect to whatever orientation you're in, i.e. in landscape you would get 568 x 320.
If you want to get the bounds in portrait (like before), use:
[UIScreen mainScreen].fixedCoordinateSpace.bounds.size
Just add launch images for the iPhone and the simulator will display the proper points. Weird bug.

How to set view size to match device screen in iOS?

I have a UIView that I need to resize to half the height of the screen, it works fine in Portrait mode but if I lunch the app with the iPad in landscape the UIView always get the screen portrait size, any ideas how to get the size at launch and after change in orientation?
I try the following but always get protrait where width = 768 and height = 1024
NSLog( #"bounds: %#", NSStringFromCGRect( [UIScreen mainScreen].bounds ) );
NSLog( #"frame: %#", NSStringFromCGRect( [UIScreen mainScreen].applicationFrame ) );
Try to set property autoresizingMask of your view to UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin.
Or try to place with its value. After setting this value your view will automatically resize after device rotation.

Resources