Trying to detect iPhone 6 Plus in landscape orientation - ios

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

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.

UIButton does not position itself correctly and not showing up in some devices - Objective C

I'm trying to align in center two buttons in a single-view application. I don't want to use auto-layout, i try to solve this case programmatically. So in my ViewController.m, in the ViewDidLoad() function, i've put the following code:
_firstButton.center = CGPointMake(self.view.center.x, self.view.frame.size.height/2.0-235);
_secondButton.center = CGPointMake(self.view.center.x, _secondButton.frame.size.height/2.0+(self.view.frame.size.height*0.9));
The secondButton is correctly positioned in any screen, on any device and on any simulator. The firstButton is positioned right only in iphone 5 and 5S simulators, but it does not even show up in my iPhone 5C device or the iPhone 6 & 6 Plus simulators.
What am i doing wrong, what do i miss here?
Thanks in advance
ps: objective-c noob here, sorry if that's a too dumb question
In viewDidLoad the frame is not set yet. I recommend you to do it in viewWillAppear
Be sure it is NOT a dumb question.
As a little help, I can tell you I find easier to handles layout with main xib with auto layout.
Otherwise, for multiple screens sizes, you could manage sizes or positions as relatives to actual screen size : UIScreen.mainScreen().bounds, for instance, in case of sizes :
let screenSize: CGRect = UIScreen.mainScreen().bounds
let sreenHeight = screenSize.height
let screenWidth = screenSize.width
let cardwidth = screenSize.width * 0.47
let cardheight = screenSize.height * 0.47
let ViewBackGround = UIView(frame:CGRectMake(0, 0, cardwidth * 0.8, cardwidth))

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.

Maximum height of iOS 8 Today Extension?

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.

Resize TextView for 4" display? [duplicate]

This question already has answers here:
How to develop or migrate apps for iPhone 5 screen resolution?
(30 answers)
Closed 8 years ago.
How do I resize a text view so that it expands when displayed in the new 4" screens that it isn't shorter? I'm using AutoLayout. Also, I calculated that since the iOS keyboard takes up 216 points on both devices, and since the Navigation bar takes up 44 points on both devices, that the remaining view would be 200 points on a 320x460 screen. But how do I set this relative to the iPhone 5 screen? Any help would be appreciated. I've provided a screenshot to illustrate what I mean to better understand my issue.
Normal Screen (Works great!):
iPhone 5 Screen (Not so great!):
Try do like this:
self.view.autoresizesSubviews = YES;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Just ended up disabling autolayout for that specific view controller, and set my own CGRect.
CGRect largeScreen = CGRectMake(0, 1, 640, 290);
CGRect screenBounds = [[UIScreen mainScreen]bounds];
if (screenBounds.size.height==568)
{
[self.htmlText setTranslatesAutoresizingMaskIntoConstraints:YES];
htmlText.frame = largeScreen;
}
It is quite simple calculate height of textfield like this
CGFloat TextFieldHeight = self.view.frame.size.height - keyboardHeight- statusbarheight - navigationBarHeight;
Then assign this height to your textfield.
CGRect TextFieldNewFrame = textField.frame;
TextFieldNewFrame.size,height = TextFieldHeight;
textField.frame = TextFieldNewFrame;

Resources