I am trying to create Universal application using iOS storyboard within Xcode 7 and iOS 9. In the storyboard concept I can see lots of methods (autolayout, auto constraints, adaptive UI, etc.). I am confused by those methods of design. Please give me a solution for image view show on center for all iPhone and iPad.
Its Quite easy ..
1.place the uiimageview to center
Then add the Below constraints
1.centre horizontal to the view && centre vertical to the view
2.set it to aspect ratio
See the image given below.
centre horizontal ,centre vertical ,aspect ratio set this three you will get ...
You can try size classes work with many different screen sizes.
Change constraint like this or other
If you want to design Universal application in iOS then you must add constraints base on the percentage(%) and There are few situations or cases for which you can not added the constraints from the Starboard but don't wary about that we can do that programatically.
Have you ever try KVConstraintExtensionsMaster library to apply constraints that I have implemented. This library having the method that will update , apply , access and remove the constrains those are applied either applied by Programmatically or from Interface Builder(StoryBoard/xib).
Programmatically imageView show on center for all iPhone and iPad.
- (void)viewDidLoad
{
[super viewDidLoad];
/* preparing View Hierarchy */
UIImageView *imageView = [UIImageView prepareNewViewForAutoLayout];
[self.view addSubview:imageView];
[imageView setBackgroundColor:[UIColor blueColor]];
/* applying constraints */
[imageView applyConstraintForCenterInSuperview];
/* Change the Ratio 0.0 to 1.0 to vary the height and width */
[imageView applyEqualHeightRatioPinConstrainToSuperview:0.8];
[imageView applyEqualWidthRatioPinConstrainToSuperview:0.8];
/* Comment the above both RatioPinConstrainToSuperview methods and uncomment below methods and run on both iPhone or iPad and see the magic.*/
/*
[imageView applyWidthConstraint:280]; // fixed width for iphone
[imageView applyHeightConstrain:280]; // fixed height for iphone
// update width constraint with proper iPad ratio
[imageView updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeWidth];
// update height constraint with proper iPad ratio
[imageView updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeHeight];
*/
}
From StoryBoard imageView show on center for all iPhone and iPad.
Step 1. Drag and place imageView in view of ViewController.
Step 2. Now Add constraint Horizontal Center in Container and Vertical center in Container.
Step 3. Add constraint Equal Width and Equal Height.
Step 4. Now update multiplier of Equal Width constraint and Equal Height.
Related
I am developing an app in which i am showing a image view.There are some red dots on image which are UIButton.When user tap on button then image should zoom.For that i have added a scroll view with constraints as below
Inside scroll view i have imageview
Now i when my screen size changes then image size also changes.I have added a UIButton as red dot.I have given it's constraints horizontal & vertical line.
Now when device size change then button position on image also change.
What i want the button position should also be same on all device even if the image size grows.Please tell me how can i do that.
Add the button on top of the UIImage View
then add constraints by dragging from the button to the image.
first put two constraints for leading space from UIImageView and top
space from UIImageView to be a constant or standard depending on ur
needs.
It would look something like this as ur code..
button.top = image.top
button.leading = image.leading
adding these two constraints will do ur work.
If u want to fix the size of the button u need to add hieght and
width constraints.
ill attach an image on how it looks like
here the weather icon is the image and i m setting constraints on the button
(remove the constraints for trailing and bottom)
To place the button according to the image
set the image to a known size say 100 x 100
and then place the button at the required place.
now check the distance from the top of the image to the button.
ex. if it is say 12 then ur ratio will be 12:100
now set a constant (button.top) to 12.
so now if the height changes to say 200 multiply it by the ratio
i.e image.height* 12/100
in the given example constant = 200(new image hieght) *(12/100)(ratio) = 24
so the new distance of the button.top = 24
Like a view, you can drag and drop the constraints into the viewController.m file:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *myLayoutConstraint;
where you can manipulate programmatically:
[UIView animateWithDuration:1.0 animations:^{
self.myLayoutConstraint.constant = 10;
} completion:^(BOOL finished) {
//handle completion
}];
I am trying to create button Image within iPhone and iPad bottom center but button sizes not changing iPad. Its showing iPhone sizes only. Please help me to create my scenario. I am using different button Image sizes for my Universal app.
First give aspect ratio to the button, so that as per device size it will increase the button height and width as per given ratio. Follow the screen shot to give aspect ratio.
Now to make button in center of the view give horizontally center in container, see in the screenshot.
Took the above image as example:
1.i know you already done centre horizontal to view constraints.
2.add leading constraints to view.
3.add trailing constraints to view.
4.add bottom space constraints to view.
5.don't set any height and width as of now.
now just run it you will get the exact requirements.......
Hope This will help you..
You must add constraints base on the percentage(%) so that button image can vary according to device(iPhone/iPad) screen size.
Constraints from StoryBoard on button
Step 1. Drag button and place it on its proper position.
Step 2. Now Add constraint Horizontal Center in Container.
Step 2. Now Add Bottom Pin constraint.
Step 3. Add constraint Equal Width and Equal Height.
Step 4. Now update multiplier of Equal Width constraint and Equal Height.
Programmatically by using the KVConstraintExtensionsMaster that will update the constraints on iPad.
- (void)viewDidLoad
{
[super viewDidLoad];
/* preparing View Hierarchy */
UIButton *button = [UIButton prepareNewViewForAutoLayout];
[self.view addSubview:button];
/* applying constraints */
[button applyConstraintForHorizontallyCenterInSuperview];
[button applyBottomPinConstraintToSuperviewWithPadding:defualtConstant];
[button applyWidthConstraint:280]; // fixed width for iphone
[button applyHeightConstrain:40]; // fixed height for iphone
// update width constraint with proper iPad ratio
[button updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeWidth];
// update height constraint with proper iPad ratio
[button updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeHeight];
/*
// update Height constraints at any specific ratio on iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[button updateAppliedConstraintConstantValueByAttribute:NSLayoutAttributeHeight withConstantRatio:1.8];
}
*/
}
I have a view named myView which is always half the screen and sit at the top of screen.
Inside this view I added an UIImageView with 120x120 size which sits in center of the myView (horizontally and vertically). Inside the IB, to satisfy the constraints(X & Y) I always need to set a fixed width and height for my image, after I set center horizontally and center vertically.
But with a fixed height and width, the image doesn't resize when changing the screen size. I want my image to resize when running on iPhone5 or iPhone 4s, because myView will resize.
I need something like the image should depend on the myView size.
How to actually achieve this ?
set imageView.clipToBounds = yes, and also set imageView.contentMode = UIViewContentModeScaleAspectFit;
Update
You are talking about imageView not the image, in that case it will not resize due to constant width and height, do one thing, create IBoutLet of constraint and change there values when required, or you can also set Aspect ratio with superView
If you set the image with fixed width and height it will not change respectively.
You can either set it to be relative to myView or you can instead set leading and trailing size from all for edges and delete the center constrains.
Your constraints should look like this:
For Swift 4: Updating answer of #Adnan Aftab
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit
How to create ScrollView with AutoLayout in Xcode 6.3. While I create the scroll view it takes 600X 600 screen and doesnt change the view size according to the screen orientation. What is the best waay to acheive the ScrollView with AutoLayout . I have following code inside ViewDidLoad
[mainScrollView addSubview:contentView];
[mainScrollView setContentSize:CGSizeMake(320,800)];
contentView.frame = CGRectMake(0, 0, [Util window_width], 800);
I have following Nib file as shown below
I have got the contentview
UIScrollView is placed over UIViewController
Give the scroll view spacing constraints to all sides of the controller's view, then it will be whatever size the screen is.
You should never just drop a view into a controller without adding your own constraints when you're using the wAny hAny size class; the system will add constraints for you, and they will be origin at {0,0}, and width and height of 600 (assuming that you made the view full size) which doesn't correspond to any actual device.
How can I adapt an UITableView height to iPhone 4-inch screen? I have the following configuration, which works for iPhone 4. However on iPhone 5 (see image below) there is a blank space because the tableview does not rezise. The tableview is scrollable.
I've autolaout enabled and I tried defining horizontal and vertical spacing constraints for all views and the tableview, but I get the same results (see image below). Any suggestion?
This viewcontroller is placed inside a placeholder since I created a custom menu. I tried settting the constant of the placeholder in the parent view with the following code. Also not working, the tableview is clipped.
-(void) viewDidLoad{
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
NSLog(#"iphone5");
self.dynamicTVHeight.constant = 465;
[self.placeholder needsUpdateConstraints];
}
else{
NSLog(#"iphone4");
self.dynamicTVHeight.constant = 375;
[self.placeholder needsUpdateConstraints];
}
And these are the constraints of my parentview, which implements a custom navigation menu. I did two tests:
With the vertical and horizontal spacing constraints:
And with the height constraint in my placeholder:
You need to set the bottomSpaceToContainer (or Bottom layout) to 0.
Make sure that there are no warnings or errors in your nib file (indicated by the yellow or red sign on your UITableView). If there are none, make sure you have all 4 constraints needed to fully describe your UITableView's position.
What you want :
To compare iphone 4 and iphone 5 :
Hope that will help =)