Changing object dimensions for different resolutions - iphone

I am a complete newbie at objective-C and have somehow managed to hack my way through making my first app. Well almost.
I built my app 4inch/retina and am now making it backward compatible to 3.5. I spent a couple hours messing with constraints and couldn't seem to get it working. My question - is it possible to change an object parameters according to phone resolution with code? Something like...
if(3.5 inch screen){
object.height= 250px;
object.x= 250px;
object.y= 250px;
}else{
object.height= 350px;
object.x= 250px;
object.y= 250px;
}
Thanks for any help.

This is what I use
CGRect screenBounds = [[UIScreen mainScreen] bounds];
int screen_h = self.view.frame.size.height;
int screen_w = self.view.frame.size.width;
if (screenBounds.size.height == 568) {
// code for 4-inch screen
}
else {
// code for 3.5-inch screen
}

This is wrong. You shouldn't change height based on the screen size.
Instead set fixed margins for top and bottom and make height variable.

Related

Swift - programmatically set UIScrollView width to 100%

In the designer I added a Scrollview, I made it have a scrollbar with this code:
svDetails.contentSize = CGSizeMake(360, 400);
However I want it to be 100% of the phone width. (leave the width unchanged).
Because right now it fits perfectly on iPhone 4s,5 and 5s but doesn't look quite right on the iPhone 6,6+ (scrollview is to small)
You can use this code:
svDetails.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)
Hope it helps.
Dharmesh Kheni' solution from above in
Swift 3:
svDetails.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height)

Progress view height in iOS 7

I want to increase the height of progress view in iOS 6 and below i am doing this using appearence method
UIImage *progressImage = [[UIImage imageNamed:#"sliderbk-progress.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 5)];
[[UIProgressView appearance] setProgressImage:progressImage];
but now in iOS7 this code is not working i even try given below code but no use. Any help will be helpfull. Thanks
[[UIProgressView appearance] setFrame:CGRectMake(20, 100, 280, 100)];
If I am understanding the question correctly it sounds like you want to increase the height of the progress view in iOS7, and the code you used previously in iOS6 is no longer working.
I had to solve a similar problem recently and I did this by adding a constraint to the progress view in Interface Builder and setting the height explicitly through the constraint. This solution will require the use of Auto-Layout, so be sure that you have that turned on.
Shown: the "Height" attribute on the Size Inspector is visibly greyed out for a Progress View and cannot be changed - however I've defined a constraint on the Progress View itself and set the constraint's height to 50 points, which is actually reflected in IB.
From what I've seen iOS6 Progress Bars have a static height value, so if you also want to support iOS6 then another approach will be necessary for that.
Whereas others have reported that a CGAffineTransform() works as well:
[self.progressView setTransform:CGAffineTransformMakeScale(1.0, 3.0)];
Already answered here
How to increase height of UIProgressView
#implementation UIProgressView (customView)
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize newSize = CGSizeMake(self.frame.size.width,9);
return newSize;
}
#end
Here's the Swift version of user3189408 and Rushabh's great solutions for newer developers and swift enthusiasts like me. Tested for iOS 7+/Swift 2.0.
progressView.transform = CGAffineTransformMakeScale(1.0, 5.0)
Swift 3.x
progressView.transform = CGAffineTransform(scaleX: 1.0, y: 5.0)
You can note that frame cannot be set by appearance accessor. You have to set it on each progress view separately.
Usually, the height is set depending on progress bar style.
- (id)initWithProgressViewStyle:(UIProgressViewStyle)style; // sets the view height according to the style
If you're using Autolayout, then the solution is simple: create a height constraint as explained by one of the answers here.
However, chances are you're here because you're creating the progress bar in code.
In this case, solving this through the transform method is not ideal if the view has round corners, since CGAffineTransform will mess with how the corner radius is drawn.
I would subclass the UIProgressView as follows:
class ProgressBarThick: UIProgressView {
var height : CGFloat = 12
var width: CGFloat = 0.0
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: width,
height: height)
}
}
Set the height and width before drawing the view.

Developing for iPhone 4 and iPhone 5 in one application

I have been away from iOS development for a bit and I am at road block.
One of my apps has a UIImageView graphic and then I have buttons that overlay the visuals. I am able to get everything working except for the autolayout.
If I set my nib to 3.5 and run the simulator in 3.5 mode everything works great. If I run it in 4.0 mode, the buttons all shift. This applies with the reverse of this also.
I'm not sure how to get around the buttons that move. Is there a way or should there be 2 nibs?
If you're not using IB then you would get the size of the view and layout based on that.
I usually use a single value for the verticalOffset (NSInteger) and base Y values on that with different multiples of the value depending on the interface element.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) { // iPhone 5
verticalOffset = 20;
} else {
verticalOffset = 0;
}
if in the future Apple releases a iPhone 5.5 with a 768 pixel height (I jest.. ha!)
You would only need to add in a new verticalOffset value and the view would instamagically fill the new size.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 768) { // iPhone 5.5
verticalOffset = 30;
} else if (screenBounds.size.height == 568) { // iPhone 5
verticalOffset = 20;
} else {
verticalOffset = 0;
}
The easy way for IB/iPhone if you want to support older iOS versions turn OFF autolayout
And turn off auto sizing for the top value.
Layout the view in IB for the iPhone 5 and when you use the iPhone 3.5in all the the interface elements will squeeze into the 3.5in size (make sure you leave enough whitespace when you lay it out in the taller size)
You can follow this good autolayout tutorial: http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2
Develop in 3.5 inch screen, but enable autoLayout. Go through each item in your view and adjust the auto sizing.
- Select item
- Go down to where the white arrows are by zoom in/out in interface builder. Click the little button that looks like an H.
- This will allow you to adjust the adjustments for each item

Best way to manage screen height differences on iPhone 5 and others

What is the best way to manage screen height differences between iPhone 5 and other versions?
If we use the storyboard or xib it will resize it accordingly. But if the whole view is build programmatically, how we take care of this issue.
Currently what I do is that I get the whole screen height and set the rest of the UI elements according to this height like height * .7 or height *.5 ..
But still it didn’t give me the best experience.
Thanks,
You can check for [UIScreen mainScreen].bounds.size.height(may be, use it as a static method or do a #define) and do the screen design based on the screen height. Another way is to use the view.autoresizingMask property to adjust the views.
for eg:-
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
If you are building for iOS 6, you can use Auto layout features.
In your viewDidLoad just check for screen size Like this...
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
//iPhone 5
}
else
{
// iPhone 4
}
}

How to set the frame for iphone 4 and iphone 5 in ios6

I want to update my application to ios 6. so how to set the frame size comfortable both iphone 4 and iphone 5?
I can using Calculate the Screen size then to adjust the height of the view like using following method
+(CGFloat)heightOf:(CGFloat)heightValue{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
//NSLog(#"applciation Frame height = %f",applicationFrame.size.height);
CGFloat heightRatio = (heightValue/480)*100;
CGFloat height = (applicationFrame.size.height * heightRatio)/100;
return height;
}
then I am using like as
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,[Height heightOf:385], 320, 220)];
and so.
Thanks for your Replies
There is more than one way of handling the situation.
1> One of them is to use auto-resizing properly.
2>Another way is to use the new Auto Layout functionality, but then again this will only be supported for iOS 6.
3>You can create separate nib's for each view one for iphone 4 and iphone 5 and depending on which device it is running, you can switch the used .xib files.
Best way right now to go about is to use, auto resizing properly.
For more you can go through these
1> Supporting iOS 4.3 to iOS 6.0
2> What is the best way for supporting both screen resolution of iPhone4 and iPhone5 ? - Auto layout in only iOS6
Following code check for the iPhone 5 and other device
#define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:#"iPhone"] )
#define IS_IPOD ( [[[UIDevice currentDevice ] model] isEqualToString:#"iPod touch"] )
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )
And check for more detail refer the question.
u can check the device type link or you can check if the view frame larger than the normal one in iPhone 4/4s then resize your frame
note: i'm use the second way and it is work
AutoLayout did not work out so well for me. My app has several elements which need to be spaced out evenly.
A Quick fix solution (not good practice) is to assign coordinates individually upon the screen height check.
(BTW that is a good tutorial. Shame Apple still haven't come up with anything substantial for IOS).
If You have several element on your screen, then I believe you'll have to re-place them individually
CGFloat height = [UIscreen mainScreen].bounds.size.height;
if(height==568.00){
self.imageName.frame = CGRectMake(x,y,width,height);
...
}
For a modal or floating screen which appears in the center of window, you could try this:
self.imgBadgeArea.frame = CGRectMake(self.view.bounds.size.width/2- self.imgBadgeArea.bounds.size.width/2, self.view.bounds.size.height/2 - self.imgBadgeArea.bounds.size.height/2, self.imgBadgeArea.bounds.size.width, self.imgBadgeArea.bounds.size.height);
Works for both 3.5 and 4 inches.