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
}];
Related
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];
}
*/
}
Hello guys i have one that is.
I have two buttons in controller when i remove or hidden one button i want to other button to increase size according to width.
Here i am using the auto layout so please post your answer accordingly
See the above image when i hide the button other button it should be adjust with the size.
set up all the constraints needed to create the layout like in your first screenshot
in IB you select the "time-button" and the "pay by cash" button and add a constraint to align their trailing edges
select this new "trailing-edges-constraint" and set it's priority to high (750)
(repeat the steps 1-3 for the "pay now" button but this time align to the leading edges)
and that's it!
FDStackView
Use UIStackView as if it supports iOS 6. It will automatically replace the symbol for UIStackView into FDStackView at runtime before iOS 9.
Or you can using a array of UIView to hold the buttons you want. The first and last element of the array is a 1px width 0.01 alpha UIView. Making the first's left align to the left of screen and the last's right align to the right of screen.
The all you need to do is that put the buttons in the array and make sure every buttons are between the 1px views.
you can add this constraint manually by creating its outlet of "width constraint" like below :
And wherever you want to re-set its width then you can do it manually,
- (IBAction)buttonClicked:(id)sender
{
[UIView animateWithDuration:0.15 animations:^{
_widthConstraint.constant = 100;
}];
[self.view layoutIfNeeded];
[self.view updateConstraints];
}
I would like to slide in a UIButton (grey color in screenshot) from the left to right and center it on the screen. There is an UIButton (orange color in screenshot) already though that is centered and this button should be pushed to the right and always have a distance of 20 from the UIButton that slides in.
The centered button (in orange) has four constraints:
Height constraint
Width constraint
Top space to Layout Guide constraint
Horizontal constraint
Currently the button that will slide in (in grey) has the following constraints:
Center Y relationship to the orange button
Height constraint
Width constraint
Leading space to superview
The idea that I had was to add an outlet to the grey UIButton's "leading space" constraint and to animate that using the UIView's animateWithDuration:animations: method:
- (void)viewDidLoad {
[super viewDidLoad];
self.slideInLeadingConstraint.constant = 100;
[self.slideInButton setNeedsUpdateConstraints];
[UIView animateWithDuration:3.0 animations:^{
[self.slideInButton layoutIfNeeded];
[self.centerButton layoutIfNeeded];
}];
}
What could I change in the constraint for this to work?
Since you would expect the orange button to move at some point, you can't force it to be centered. Lower the priority from 1000 to something lower. This means: I would really like it to be centered, but it doesn't have to be.
Add a horizontal distance (leading/trailing) constraint between the two buttons. Set its value to >= 20. This means: The distance cannot go under 20pt, but anything larger is fine.
Animate away ;)
Inside your animation block, you need to call layoutIfNeeded on all views involved in the animation or on one of their common superviews.
Note:
The reason why you had problems with this is that you have called it from viewDidLoad. At that point, the view has been fully initialized, but not added to the hierarchy yet.
It's not yet certain, how big the parent view will be and dimensions for the animation will have to be guessed (by just taking the dimensions of your scene in the storyboard).
Performing the animation after it has been initially displayed (e.g. in viewDidAppear) causes the animation to be calculated with the right dimensions.
I defined a view which contains an UImageView and a UILabel. I setted constraints for each elements.
In wanted to do a circle with the first view, so I did that in the code :
self.mainView.layer.cornerRadius = self.mainView.frame.size.width / 2;
self.mainView.clipsToBounds = YES;
So it works, I have a circle, BUT the UImageView and the UILabel seems don't follow the constraints setted in the storyboard.
For example, on my UILabel, I setted a margin left and right 5px to the mainView, but I can see my UILabel "out" the mainView....
I tried to do a "setNeedsUpdateConstraints", but it's not the solution.
So, what I need to do to have my UILabel correctly positioned ?
Thanks,
I think this is what you are looking for:
self.mainView.layer.masksToBounds = YES;
Your problem is that when changing the mainView's layer you are not actually changing the bounds of the view. The bounds of the view are still represented in a rectangle manner. what you need to do is change the width constraint of the UILable.
To do so just create a autolayout constrain to your UILable (is you don't have it already). Control-Drag it to your viewController and change it dynamically using the constant value in the constraint.
If you don't need to set it dynamically just set the left & right margins to a bigger margin
I'm working on a view that contains three UIImageViews. The view and the child imageviews are created in the storyboard. The images for these imageviews are loaded from a server.
When the server returns images for all three images, it should look like this:
When there is no image available for the third imageview, the layout should dynamically change like this (with image scaling into the imageview etc, I know that part):
I am already working quite a while with the Interface Builder, but have no idea how to achieve this..
Anyone can help me out figuring this, preferred using autosizing in IB?
This kind of possible using autolayout, but you still have to modify one constraint programatically, based on whether you are displaying one or two UIImageViews.
Here's how you do it, with autolayout:
Create The Container View
Create a UIView as a container.
Move the left edge of the container view to the left margin.
Move the right edge of the container view to the right margin.
Pin the container view's height.
Delete the constraint for the bottom space to superview.
Create The UIImageViews
Create the three UIImageViews inside the container view.
Size and align the UIImage views as you want them to display.
Pin the horizontal spacing between the first and second image view.
Pin the horizontal spacing between the second and third image view.
Pin the leading space on the first image view to the superview.
Pin the trailing space on the third image view to the superview.
Select all three image views and pin the widths equally.
Delete the align center x constraint on the second image view.
Select the trailing space to image view constraint on the second image view and set the priority to 900 (important).
Create the Magic Extra Constraint
Pin the trailing space to superview on the second image view.
Select the new constraint and set the priority to 850.
Set the constant value for the constraint to 0.
The container UIView is simply to maintain the margins and height for the containing views.
The second image view should now have two trailing constraints, both appear as dotted lines. Rotating the screen should have the container view stretching to fit, and the three UIImageViews also stretch to fit.
Currently, the constraint between the second and third image views is higher priority than the "magic" constraint between the second image view and the container view, causing the second image view's right edge to be spaced away from the third image view's left edge. To adjust the layout for just two images, the "magic" constraint has to be higher priority than the other one, causing the second image view's right edge to align with the superview's right edge.
Simply create an IBOutlet for the "magic" constraint (trailing space to superview on the second image view) and raise and lower the priority as needed.
if (imageCount == 2) {
// Second image view should favor right edge of superview
self.myConstraint.priority = 950;
} else if (imageCount == 3) {
// Second image view should favor left edge of third image view
self.myConstraint.priority = 850;
}
You may also wish to hide the third image view when necessary.
My option would be that when third image is null set the isHidden property of third imageView to YES. Then specify the bounds of the other two imageviews accordingly(which, you said, you know how to do).
Check if imageView_3 have no image and then adjust size of rest two imageViews such as:
if (imageView_3.image == nil) {
CGRect frame = imageView_1.frame;
frame.size.width += imageView_3.frame.size.width / 2;
imageView_1.frame = frame;
frame = imageView_2.frame;
frame.size.width += imageView_3.frame.size.width / 2;
frame.origin.x += imageView_1.frame.origin.x + imageView_1.frame.size.width + 10/*the gap*/;
imageView_2.frame = frame;
}
In my opinion you can check the image count coming from server that is whether there is three image or two image by counting the array count. Then you can set different frame to UIImageView based on the array count.
if([imageArray count] == 3) {
//set the frame of the images so that it can fit the screen.
}
else if([imageArray count] == 2) {
// set the frame of the UIImageView so that it can fit the screen and hide the third UIImageView
[thirdImageView setHidden:YES];
}
Like this you can set the frame for UIImageView