I'm making an iPhone app, and I'm using storyboard for the majority of the UI. I'm using xCode 4.6 for iOS 6.
Is there a way to make the app work for both iphone4 size and iphone5 sizes?
When I run the app on the iPhone 4 simulator it doesn't look like it's supposed to - the UI elements don't look like they do on the storyboard (which I'm assuming uses the iPhone5 size).
Let me know if you need more info.
You can dynamically access the height of the view and the device, and then make adjustments based on those values. Alternatively, you could use iOS 6's constraints to set a margin between the bottom of the device or between other elements.
To achieve the former, just access the height property of the view:
CGFloat height = [[self view] frame].size.height;
You can also get the height of the device's screen like so:
CGFloat deviceHeight = [[UIScreen mainScreen] bounds].size.height;
deviceHeight -= 20; // remove the tab bar
deviceHeight -= 44; // remove height for a navigation bar?
Now imagine adjust your view's origin based on this value. You can make it hug the bottom of the device, no matter which one you're on.
[aView setFrame:CGRectMake(10, deviceHeight - 10 - 100, 300, 100)];
If you're unable to adjust the layout of the elements, consider using a scroll view as well. Just set the frame using the techniques above, and then set the content size. On smaller devices, you'll be able to scroll to see more content whereas on larger devices, it will all be right there.
Related
How can i modify my code so same app can run on iphone with same UI elements.
I made changes in info Plist and made it universal but Problem is that i have not used stroryboard or nib , xib files to display UI elements. also i hardcoded the height and width for particular button or label ,frame etc.
So is there any way to auto resize screen to ipad view to iphone Or I need to write every code with new height and width for iPhone?
like in android i detect the height and width and according to that i divide it by 2 or 3 to place particular UI item on different screen size.
Also how can i detect that app is running on iphone 4 or iphone 5 or iPad so i can change my images and value or height n width?
You can get the screen bounds using:
[[UIScreen mainScreen] bounds];
The quantity of changes that you would have to make depends on how you designed your code before, if you were using absolute values all the times you'll have much more problems than if you were using relative values...
you can take a look at the UIViewAutoresizingMask property of your views, for example:
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
as to how to check if you are on iPhone 5 you can use this line of code :
BOOL isIPhone5 = [[UIScreen mainScreen ] bounds ].size.height == 568 ? YES : NO ;
Create seperate nibs for iphone and ipad
well doing it all codewise then you have to set the frame for all the components code wise checking the device is ipad or iphone.
Checking iPad or iPhone look here
I am workin on an iPhone app and am relatively new to xcode. I am trying to design my application so that no matter what device is used, the main ui will be center. I have a uiview that contains all of my controls (textfields, submit button). I have this uiview centered in interface builder based on the iPhone 5 simulated metrics. When I test this on an iPhone 4 or below, naturally this uiview is not centered but towards the bottom of the screen. Is there a simple way witin interface builder to have this uiview autocenter based on the device? Do I need to add this in programmatically?
Thanks to anyone who can help!
iPhone 3.5inch = http://bit.ly/14r6R6U
iPhone 4inch = http://bit.ly/WOyQcn
You can use the auto resizing option in the interface builder. Fix the top margin and bottom margin and set flexible height
If you are using Xcode 4.5 onward then you can make two different xibs for iPhone 3.5 inches and iPhone 4 inches like:
this will easy to maintain all view components .
Initially create the baseView with the size of (320,480) then add the view as subview of the baseview.
then Do like this,
CGRect _baseFrame=baseView.frame;
_baseFrame.origin=CGPointMake((self.view.frame.size.width-baseView.frame.size.width)/2, (self.view.frame.size.height-baseView.frame.size.height)/2);
baseView.frame=_baseFrame;
If you have auto layout turned on (it is by default), you should center the view both horizontally and vertically, and give it a fixed height and fixed width. Once you have these, you should be able to delete any other constraints. This will keep it centered in any sized screen in portrait and landscape.
You need to explore these things:
The old way - Autosizing masks
The new way - Auto layout
This is enabled by default on new projects. You can turn it off by unchecking a box in the file inspector for the storyboard. Auto layout is ios6 only. Autosizing works on ios6,5,4...
Both of these provide layout engines that do not require code. With both methods you change the settings of objects in the storyboard using the size inspector.
Autolayout is Apple's strongly preferred way forward, but I would recommend trying autoresizing masks first. Autolayout allows you to exert far more control over relative positioning of objects, at the cost of far greater complexity. If you can achieve your result using autosizing, do.
Raywenderlich.com has a good guide to both.
define its in your view controller
#define STATUSBAR_HEIGHT 20
#define SCREEN_HEIGHT (( double )[ [ UIScreen mainScreen ] bounds ].size.height - STATUSBAR_HEIGHT)
#define SCREEN_WIDTH (( double )[ [ UIScreen mainScreen ] bounds ].size.width)
And for example
UIView *view = [[UIView alloc]init];
[view setBackgroundColor:[UIColor purpleColor]];
//for example view height 100 and width 100 then
float xAxis = (SCREEN_WIDTH - 100)/2;
float yAxis = (SCREEN_HEIGHT - 100)/2;
[view setFrame:CGRectMake(xAxis, yAxis, 100, 100)];
[self.view addSubview:view];
And if you making view by xib, then you should use this
In XCode interface builder : I have some images in my view that I need them to be center aligned vertically.
But screen height of Retina 4 and Retina 3.5 differs and I don't like to use OS6 for relative coordinates.
Is there anyway to do this is interface builder or I should write some code?
Using Autolayout in interface builder, select your view, choose the constraints menu in the bottom right of the canvas, and choose center vertically in container. This will apply to both screen sizes.
You should probably use actual code for this.
Luckily, it's fairly straightforward: just obtain the screen height, subtract the image height, and divide by two.
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
CGRect frame = [imageView frame];
frame.origin.y = (height-frame.size.height)/2;
[imageView setFrame:frame];
You can use autoresizing(IB->Size inspector) for it. But you will not see autoresizing options if auto layout is set for xib. You have to remove auto layout(IB->File inspector) first & then set autoresizing properties as you want.
I'm updating an older project to support the iPhone/iPod 5's 4 inch screen. I've added the Default-568h#2x.png to my project, and almost everything seems fine, except:
the Game Center overlay (view controller) is cut off at the bottom.
Tweet sheet popups aren't full width.
Touch input isn't being received on the right/bottom side of the screen.
Seems like my window isn't the correct length.
(I'm going to propose a solution, but I'd like some feedback as to whether or not this is a safe (or "correct") solution.)
I've manually edited my MainWindow.xib file, changing the NSFrameSize from {320, 480} to {320, 568}, which has solved the apparent problem.
This doesn't seem to be causing problems on older/smaller screens, but I'm wondering if this is the best across-the-board solution...
shouldn't you get the current screen size as in the height using following and then use it
instead of changing it manually
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
I am using in my app the view size to position a subview in code.
for example like this:
self.view.bounds.size.height
This subview has to be animated in and out of the view.
This has worked perfect on older devices, now I am trying to support iphone 5 and found out that I still get the height of the old devices.
Everything except this animated view adapts perfect for iphone 5.
The only way to get the right size is if I change the size of the view in the xib, downside is
that if I now run my app on iPhone 4 the view size is the 4inch view size.
What is the problem here?
Or is this the way it is supposed to be and I have to create an extra xib file for iPhone 5?
Alright so the answer is the following:
Like #mrwalker said make sure that the view automatically or programmatically resizes.
And be aware of the fact that the view is not yet resized in the viedDidLoad method.
(This was my mistake)
If you need the resized views size do your stuff in viewWillAppear there the view has already the right size.
thanks to #mrwalker and #AndyDev
You need to make sure your view is resized for the device it's running on. You could either:
Create a new Xib for the iPhone 5, as you might for an iPad
Have existing Xib automatically or programatically resize
I would only opt for (1) if you were intending on having a different layout (more / fewer buttons and such).
How you achieve (2) depends on whether you're using iOS 6's Auto Layout or the old autoresize model. Both methods can be controlled in the Utilities > Size Inspector in Xcode, or programatically.
If you have a single view & view controller, allowing the view to automatically resize to the parent window should be enough.
I had a similar issue where I wasn't getting the correct height and using the Autolayout / autoresize didn't achieve the desired effect. I used the following code to determine the screen size and made the changes based on this.
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)]) {
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 1136){
// iPhone 5 (1136px height)
} else {
// Not iPhone 5
}
}