Ios Animation of UIImageView with Constraints - ios

I have an UIImageView which I placed in the center of screen using Align Center X and Align Center Y constraints.I want the image to move with animation to the top of screen keep the horizontal alignment in container and have a space of 20 from the top when i press a button. Remove the Align Center Y constraint programmatically and add the top space constraint is a good approach for that?

Calculate dynamically the constant with the UIScreen class and your view height.
#interface ViewController : UIViewController
#property (nonatomic, weak) IBOutlet NSLayoutConstraint *centerYConstraint;
#property (nonatomic, weak) IBOutlet UIView *myView;
- (IBAction)moveView:(id)sender;
#end
- (IBAction)moveView:(id)sender
{
self.centerYConstraint.constant = 20.0 + (self.myView.frame.size.height * 0.5) - ([UIScreen mainScreen].bounds.size.height * 0.5);
[UIView animateWithDuration:1.0 animations:^{
[self.view layoutIfNeeded];
}];
}
You can download the code here (http://cl.ly/3l1B0k1L0h1C)

Related

Change height of view programmatically in uistackview

I need to change the view height in the stack view when I press the test button, but it is not working properly.
When I press the test button, I want to set the height of view 3 to 50 and the height of view5 to fill the remaining area. When I press the test button again, i want to reverse to process. How can I do that?
Thank you.
As #SeanLintern88 mentioned, the way you really should be doing this is with auto layout constraints -- you don't want to be mixing setFrame with autolayout.
IBOutlet the height constraints for View 3 and View 5. Set the View 3 height constraint as inactive to start (if you want it to look like your storyboard does currently to start), then whenever the button is pressed, check which constraint is active and flip-flop them.
#import "ViewController.h"
#interface ViewController ()
#property (strong, nullable) IBOutlet NSLayoutConstraint *view3HeightConstraint;
#property (strong, nullable) IBOutlet NSLayoutConstraint *view5HeightConstraint;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// start us out as inactive
self.view3HeightConstraint.active = NO;
}
- (IBAction)btnPressed:(id)sender {
if (self.view5HeightConstraint.active) {
// view 5 height constraint is active
// you can set the height constants directly in storyboard as well
self.view3HeightConstraint.constant = 50.0f;
self.view3HeightConstraint.active = YES;
self.view5HeightConstraint.active = NO;
} else {
// view 3 is height constraint is active
// you can set the height constants directly in storyboard as well
self.view5HeightConstraint.constant = 50.0f;
self.view5HeightConstraint.active = YES;
self.view3HeightConstraint.active = NO;
}
// animate the layoutIfNeeded so we can get a smooth animation transition
[UIView animateWithDuration:1.0f animations:^{
[self.view layoutIfNeeded];
}];
}
#end

HidesBottomBarOnPush iOS10 safe area pinning issue

I'm having an issue with the safe area on iOS 10 where I trigger a segue to a view controller with hidesBottomBarOnPush enabled. The content which is pinned to the bottom safe area on this view controller starts off above the tabs then jumps to the bottom once the view has fully loaded.
How do I avoid this behaviour on iOS 10? Pinning to superview is not an option as iPhone X support is required.
Constraints on the label:
Pinning to superview, bind to the attribute, like this
#interface YourViewController ()
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
#end
#implementation YourViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat bottomValue = 0.0f; // your value
UIEdgeInsets edgeInsets = UIApplication.sharedApplication.keyWindow.layoutMargins;
CGFloat bottomInset = edgeInsets.bottom;
self.bottomConstraint.constant = - bottomInset - bottomValue;
}
#end

Resize xib UIView to fit UIButton inside

I have a UIView created from a xib file. Inside that UIView there is a UIButton with a text that may be longer or shorter depending on the language.
What I want is to resize the parent UIView to fit the width of the UIButton. How can I do it with AutoLayout, or programmatically?
Thanks,
add leading, trailing, top and bottom constraint of button
add width constraint of view and connect to viewWidthConstraint.
calculate width of button for language and set constant of viewWidthConstraint.
View and button will resize
#property (nonatomic, weak) IBOutlet NSLayoutConstraints *viewWidthConstraint;
- (void)viewDidLoad {
super.viewDidLoad();
self.viewWidthConstraint.constant = [self buttonWidth];
}
- (CGFloat)buttonWidth {
return 100.f; (calculate width of button)
}

get x y location of a textbox inside scrollview

The layout of my UIViewController is :
Layer 1:UIView (height is 568)
Layer 2:UIScrollView (height is 2000)
Layer 3:UIView (height is 2000)
i have lot of textfield inside this layer 3 UIView, i would like to know the current x y showing on layer1 UIView (not the layer 3 UIView original x y)
should i use convertPoint ? I got invalid results
-(void)textFieldDidBeginEditing:(UITextField *)textField {
CGPoint newPoint = [textField convertPoint:textField.frame.origin fromView:self.view];
NSLog(#"%f",textField.frame.origin.y);
NSLog(#"%f", newPoint.y);
}
You have to create IBOutlet for your Layer 1 view and Layer 3 view.
in your ViewController.h
#property (strong, nonatomic) IBOutlet UIView *superView;
#property (strong, nonatomic) IBOutlet UIView *subView;
in your ViewController.m
CGPoint originInSuperview = [self.superView convertPoint:textField.frame.origin fromView:self.subView];
Then you will have the SuperView x y location.
You can use superview means a view which holds the another view over it. It manages subviews. Subviews means the views which are holding/added over a 'View'.
Lets Say. you have a SuperView UIview which has a subview UIScrollView and which has another subview UIview over it.
Might be it helps you. The event-handling methods: OnTouch
UITextField *myTextField;
Get textFiled origin x and Y
myTextField.frame.origin.x;
myTextField.frame.origin.y;
Get Minmum(origin) and maximum (end) and mid point of textFiled
CGRectGetMinX(myTextField.frame);
CGRectGetMaxX(myTextField.frame);
CGRectGetMinY(myTextField.frame);
CGRectGetMaxY(myTextField.frame);
CGRectGetMidX(myTextField.frame);
CGRectGetMidY(myTextField.frame);

How can I resize UIView width when some view hidden?

The above Image is captured from storyboard.
It is constructed like as follows:
UIView (include UILabel and UIProgressView) <--2px spacing --> UIActivityIndicatorView <---6px spacing ---> UIButton
I want the UIView to increase its width to button when UIActivityIndicatorView hidden as follows:
UIView( include UILabel and UIProgressView) <---6 space---> UIButton
How can I do that?? Let me Know. Please.
https://github.com/bilobatum/ActivityIndicatorDemo
Make the button's intrinsic content size along the horizontal axis required (i.e., priority 1000). This prevents the layout from being ambiguous along the horizontal axis.
#interface ViewController ()
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *activityIndicatorWidthConstraint;
#property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
// spacerConstraint is the horizontal spacer constraint between the activity indicator and the gray view
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *spacerConstraint;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.activityIndicator startAnimating];
}
- (IBAction)stopButtonTapped:(UIButton *)sender
{
sender.enabled = NO;
self.activityIndicatorWidthConstraint.constant = 0;
self.spacerConstraint.constant = 0;
[UIView animateWithDuration:1.0 animations:^{
self.activityIndicator.alpha = 0.0;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[self.activityIndicator stopAnimating];
//[NSLayoutConstraint reportAmbiguity:nil];
}];
}
#end
If you are using Auto-layout, consider setting the width of the UIActivityIndicatorView to 0 when you disable it. This won't be exactly what you need, but it's close.
Let's assume that your UILabel is called _label, UIProgressView is called _progressView and UIActivityIndicator is called _activityIndicator.
When you hide your _activityIndicator, call this block of code:
[_label setFrame:CGRectMake(_label.frame.origin.x, _label.frame.origin.y, _label.frame.size.width + _activityIndicator.frame.size.width + 2, _label.frame.size.height)];
[_progressView setFrame:CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, _progressView.frame.size.width + _activityIndicator.frame.size.width + 2, _progressView.frame.size.height)];
When _activityIndicator shows again:
[_label setFrame:CGRectMake(_label.frame.origin.x, _label.frame.origin.y, _label.frame.size.width - _activityIndicator.frame.size.width - 2, _label.frame.size.height)];
[_progressView setFrame:CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, _progressView.frame.size.width - _activityIndicator.frame.size.width - 2, _progressView.frame.size.height)];
Remember, that Autolayout should be turned off, if you want to manually set the frames! It is very simple: just uncheck the "Use Autolayout" feature in File Inspector:

Resources