Pull down menu from Navigation bar - ios

Does anyone know of a good solution to attaching a retractable menu to the bottom of the navigation bar?
I have found this: https://github.com/mentionapp/mntpulltoreact, but this has the behavior of pulling down on the whole view, not letting go, and then selecting the item that one wants by dragging your finger along.
The Zappos app has a perfect example of what I am looking for. Does anyone know what they could have been using or an existing solution for this? I've searched on cocoacontrols and haven't found much.

Would doing something like this work for you? then of course you can just add whatever menu buttons you need to the menu view.
UIView * menu;
UIButton * activate;
int menuHeight;
int buttonHeight;
-(void)toggleMenu:(UIButton*)sender{
[sender setUserInteractionEnabled:NO];
[self.view bringSubviewToFront:menu];
if (sender.tag == 1000){
[UIView transitionWithView:menu duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect frame = CGRectMake(menu.frame.origin.x,menu.frame.origin.y - menu.frame.size.height + buttonHeight ,menu.frame.size.width,menu.frame.size.height);
menu.frame = frame;
[sender setUserInteractionEnabled:YES];
} completion:nil];
sender.tag = 2000;
} else {
[UIView transitionWithView:menu duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect frame = CGRectMake(menu.frame.origin.x,menu.frame.origin.y + menu.frame.size.height - buttonHeight ,menu.frame.size.width,menu.frame.size.height);
menu.frame = frame;
[sender setUserInteractionEnabled:YES];
} completion:nil];
sender.tag = 1000;
}
}
menuHeight = 100;
buttonHeight = 20;
activate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[activate addTarget:self action:#selector(toggleMenu:) forControlEvents:UIControlEventTouchUpInside];
[activate setTitle:#"Menu" forState:UIControlStateNormal];
activate.frame = CGRectMake(80.0, menuHeight - buttonHeight, 70.0, buttonHeight);
activate.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
activate.tag = 2000;
menu = [[UIView alloc] initWithFrame:CGRectMake(0,0 - menuHeight + buttonHeight,self.view.bounds.size.width, menuHeight)];
menu.autoresizingMask = UIViewAutoresizingFlexibleWidth;
menu.backgroundColor = [UIColor grayColor];
[self.view addSubview:menu];
[menu addSubview:activate];

you can then use some constraints to shift the table down and what not. this is untested but should get you pointed in the right direction
-(void)toggleMenu:(UIButton*)sender{
UIView * menu;
[sender setUserInteractionEnabled:NO];
[self.view bringSubviewToFront:menu];
id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;
NSDictionary * viewsDictionary;
viewsDictionary = NSDictionaryOfVariableBindings(myTable, topGuide, bottomGuide, menu);
NSArray * newVertConstraint;
if (sender.tag == 1000){
newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|[bottomGuide][menu(==%f)][myTable][bottomGuide]|", 0.0] options:0 metrics: 0 views:viewsDictionary];
sender.tag = 2000;
} else {
newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|[bottomGuide][menu(==%f)][myTable][bottomGuide]|", menu.frame.size.height] options:0 metrics: 0 views:viewsDictionary];
sender.tag = 1000;
}
[self.view removeConstraints:vertConstraint];
[self.view addConstraints:newVertConstraint];
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:1.5 animations:^{
[self.view layoutIfNeeded];
}];
vertConstraint = newVertConstraint;
}
NSArray * vertConstraint;
UITableView * myTable = [[UITableView alloc] init];
[self.view addSubview:myTable];
self.automaticallyAdjustsScrollViewInsets = NO;
[myTable setTranslatesAutoresizingMaskIntoConstraints: NO];
[menu setTranslatesAutoresizingMaskIntoConstraints: NO];
id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;
NSDictionary * viewsDictionary;
viewsDictionary = NSDictionaryOfVariableBindings(myTable, topGuide, bottomGuide, menu);
vertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[bottomGuide][menu(==0)][myTable][bottomGuide]|" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:vertConstraint];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[menu]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[myTable]|" options:0 metrics: 0 views:viewsDictionary]];

Related

How to show UIView like UIAction sheet using UIView animations in iOS?

Hi I am very new for iOS and I have added a UIView at bottom of MainViewController.
And when I tap on a button that UIView displays like "UIAction sheet" using UIView animations.
And for this I have tried the code below but animations are not applying when I tap on the button.
Please help me.
my code:-
#import "AnimationUIview.h"
#interface AnimationUIview ()
{
UIView * firstView;
NSLayoutConstraint * Bottom;
}
#end
#implementation AnimationUIview
- (void)viewDidLoad {
[super viewDidLoad];
firstView = [[UIView alloc] init];
firstView.backgroundColor = [UIColor lightGrayColor];
firstView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:firstView];
NSDictionary * views = NSDictionaryOfVariableBindings(firstView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[firstView]-0-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[firstView(100)]" options:0 metrics:nil views:views]];
Bottom = [NSLayoutConstraint constraintWithItem:firstView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:80];
[self.view addConstraint:Bottom];
}
- (IBAction)buttonAction:(id)sender {
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
Bottom.constant = -10;
}
completion:nil];
}
#end
#import "AnimationUIview.h"
#interface AnimationUIview () {
UIView * firstView;
NSLayoutConstraint * Bottom;
BOOL isshown;
}
#end
#implementation AnimationUIview
-(void)viewDidLoad {
[super viewDidLoad];
firstView = [[UIView alloc] init];
firstView.backgroundColor = [UIColor lightGrayColor];
firstView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:firstView];
NSDictionary * views = NSDictionaryOfVariableBindings(firstView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[firstView]-0-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[firstView(100)]" options:0 metrics:nil views:views]];
Bottom = [
NSLayoutConstraint
constraintWithItem:firstView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:80
];
[self.view addConstraint:Bottom];
isshown=No;
}
- (IBAction)buttonAction:(id)sender {
if (isshown==NO) {
firstview.frame = CGRectMake(0,0, SCREEN_WIDTH, 100);
[UIView animateWithDuration:0.40 animations:^{
firstview.frame = CGRectMake(0, SCREEN_HEIGHT-100, SCREEN_WIDTH, 100);
[firstview setAlpha:0.0f];
} completion:^(BOOL finished) {
}];
isshow=Yes;
} else {
firstview.frame = CGRectMake(0, SCREEN_HEIGHT-100, SCREEN_WIDTH, 100);
[UIView animateWithDuration:0.40 animations:^{
firstview.frame = CGRectMake(0,0, SCREEN_WIDTH, 100);
firstview.alpha = 1.0f;
} completion:^(BOOL finished) {
[firstview setHidden:YES];
}];
isshown=No;
}
#end

How to Push one UIview to another UIview in ios using Animation concept?

Hi I am very new in iOS and in my project I have added two UIViews programmatically on my main UIViewController.
Here my main requirement is when I click "Next" button which is in my firstView, I want to push the FirstView to SecondView and
when I click "Back" button which is in the SecondView, I want to pushBack secondView to firstView.
As in how to push one UIViewController to another UIViewController?
But same requirement I want to do Using UIView programmatically.
Please help me.
How can we do this?
my code:-
#import "ViewController.h"
#interface ViewController ()
{
UIView * FisrtView;
UIView * SecondView;
UIButton * Next;
UIButton * Back;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
FisrtView = [[UIView alloc] init];
FisrtView.backgroundColor=[UIColor lightGrayColor];
FisrtView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:FisrtView];
SecondView = [[UIView alloc] init];
SecondView.backgroundColor=[UIColor orangeColor];
SecondView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:SecondView];
Next = [[UIButton alloc] init];
Next.backgroundColor=[UIColor orangeColor];
Next.translatesAutoresizingMaskIntoConstraints = NO;
[Next setTitle:#"Go Next" forState:UIControlStateNormal];
[Next addTarget:self action:#selector(Next:) forControlEvents:UIControlEventTouchUpInside];
[FisrtView addSubview:Next];
Back = [[UIButton alloc] init];
Back.backgroundColor=[UIColor lightGrayColor];
Back.translatesAutoresizingMaskIntoConstraints = NO;
[Back setTitle:#"Go Back" forState:UIControlStateNormal];
[Back addTarget:self action:#selector(Back:) forControlEvents:UIControlEventTouchUpInside];
[SecondView addSubview:Back];
//Applting Autolayouts for All Fields
NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(FisrtView,SecondView,Next,Back);
//Appliying Autolayouts for FirstView
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-0-[FisrtView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|-0-[FisrtView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
//Appying Autolayoust for NextButton
[FisrtView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-10-[Next]-10-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[FisrtView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|-100-[Next(30)]"]
options:0
metrics:nil
views:HeaderDictionary]];
//Appliying Autolayouts for secondView
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-0-[SecondView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|-0-[SecondView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
//Appying Autolayoust for BackButton
[SecondView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-10-[Back]-10-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[SecondView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|-100-[Back(30)]"]
options:0
metrics:nil
views:HeaderDictionary]];
SecondView.hidden = YES;
FisrtView.hidden = NO;
}
-(void)Next:(id)sender{
SecondView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //your view start position
[UIView animateWithDuration:0.15f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[SecondView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; // final visible position
}
completion:nil];
SecondView.hidden = NO;
}
-(void)Back:(id)sender{
SecondView.hidden = YES;
FisrtView.hidden = NO;
}
#end
initial mistake, you were create the button is wrong and does not set the frame to visible area
not like
Next = [[UIButton alloc] init];
do like
Next = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Next.frame = CGRectMake(210, 285, 100, 18)];
Next.translatesAutoresizingMaskIntoConstraints = NO;
[Next setTitle:#"Go Next" forState:UIControlStateNormal];
[Next addTarget:self action:#selector(Next:) forControlEvents:UIControlEventTouchUpInside];
[FisrtView addSubview:Next];
and forget to add frame for visible area in view
FisrtView = [[UIView alloc] init];
FisrtView.frame = CGRectMake(210, 285, 100, 18)];
SecondView = [[UIView alloc] init];
SecondView.frame = CGRectMake(210, 285, 100, 18)];
for sample animation
-(void)Next:(id)sender{
SecondView.frame=CGRectMake(248, -420, 500, 414); //your view start position
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[SecondView setFrame:CGRectMake(248, 30, 500, 414)]; // final visible position
}
completion:nil];
}
for sample animation see this link
Please refer following link.
hope this will help you.
http://googleweblight.com/?lite_url=https://stackoverflow.com/questions/2215672/how-to-change-the-push-and-pop-animations-in-a-navigation-based-app&ei=Qc-ODruI&lc=en-IN&s=1&m=775&ts=1447514312&sig=APONPFlwklDRNHSybRHp3xVcrDKmQUeRjg

How can we reduce code using Autolayouts in ios

Hi i am beginner for auto-layouts and i am inserting "3" textfields and "1" button on scrollview
Here my requirement is according i-phone inches "Top space" must be Adjusting and when i click textfield all fields must be scrolling above keyboard and when i click "return" button in keyboard then scroll must be scrolling as like previous
For this i have tried below code but that is too length code can anybody explain me this concept using short process
my code:-
#import "ViewController10.h"
#interface ViewController10 ()
{
UIScrollView * scrollView;
UITextField * emailTextField;
UITextField * nameTextField;
UITextField * passwword;
UIButton * submit;
NSDictionary * viewsDic;
NSArray * verticalConstraints;
int height;
}
#end
#implementation ViewController10
- (void)viewDidLoad {
[super viewDidLoad];
height = [UIScreen mainScreen].bounds.size.height;
scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
emailTextField = [self createLabelWithText];
emailTextField.delegate = self;
[scrollView addSubview: emailTextField];
nameTextField = [self createLabelWithText];
nameTextField.delegate = self;
[scrollView addSubview: nameTextField];
passwword = [self createLabelWithText];
passwword.delegate = self;
[scrollView addSubview: passwword];
submit = [[UIButton alloc]init];
submit.backgroundColor = [UIColor orangeColor];
[submit setTitle: #"Submit" forState: UIControlStateNormal];
submit.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:submit];
viewsDic = NSDictionaryOfVariableBindings(scrollView,emailTextField,nameTextField,passwword,submit);
//Applying autolayouts for scrolview
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-0-[scrollView]-0-|"]
options:0
metrics:nil
views:viewsDic]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|-0-[scrollView]-0-|"]
options:0
metrics:nil
views:viewsDic]];
//Applying autolayouts for textfields and button
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:emailTextField
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
NSArray * keys = #[#"emailTextField",#"nameTextField",#"passwword",#"submit"];
for (NSString * key in keys) {
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-10-[%#]-10-|",key]
options:0
metrics:nil
views:viewsDic]];
}
if (height == 480.0) {
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-30-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
else if (height == 568.0){
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-50-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
else if(height == 667.0){
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-80-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
[scrollView addConstraints:verticalConstraints];
}
-(UITextField *)createLabelWithText{
UITextField * textfield = [[UITextField alloc] init];
textfield.textColor = [UIColor whiteColor];
textfield.backgroundColor = [UIColor lightGrayColor];
textfield.translatesAutoresizingMaskIntoConstraints = NO;
return textfield;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
scrollView.contentSize = CGSizeMake(320, 700);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
[scrollView removeConstraints:verticalConstraints];
if (height == 480.0) {
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-30-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
else if (height == 568.0){
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-50-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
else if(height == 667.0){
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-80-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
[scrollView addConstraints:verticalConstraints];
return YES;
}
#end
try masonry,it can reduce the code and it is easier for you to maintain and understand
https://github.com/SnapKit/Masonry

How to stop iOS keyboard interfering with UITextField position

I have a problem where the keyboard dismissal, I think interferes with the animation code I have. What I would like to happen is if the user clicks the search button while the keyboard is up, the keyboard drops and the text field will animate to the top of the screen.
Like in this image.
http://imgur.com/5dQ6aPZ
However, what actually happens if the search button is pressed while the keyboard is up, is this.
http://imgur.com/tp6Ffne
Here is the code for the Search button:
- (IBAction)searchButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x - 0.0, (_roomCodeInput.frame.origin.y - 105.0), _roomCodeInput.frame.size.width + 0.0, _roomCodeInput.frame.size.height);
[UITextField commitAnimations];
}
Search Again button
- (IBAction)searchAgainButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.6
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = NO;
[_srcButton setHidden:NO];
[_srcAgainButton setHidden:YES];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x + 0.0, (_roomCodeInput.frame.origin.y + 105.0), _roomCodeInput.frame.size.width - 0.0, _roomCodeInput.frame.size.height + 0.0);
[UITextField commitAnimations];
}
And here are the methods I'm using to dismiss the keyboard
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[_roomCodeInput resignFirstResponder];
return NO;
}
-(void)dismissKeyboard{
[_roomCodeInput resignFirstResponder];
}
I'm just trying to find the best solution to solve this problem or perhaps re-design to something similar.
Any Help would be greatly appreciated! :)
Replace your code as follows:
Hope it will work now
Search button:
- (IBAction)searchButton:(id)sender {
//[UITextField commitAnimations];
[UIView animateWithDuration:0.5 animations:^{
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
_roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, -100.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
}];
}];
}
Search Again button
- (IBAction)searchAgainButton:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
_enterRoomLabel.hidden = NO;
[_srcAgainButton setHidden:YES];
_roomCodeInput.text=Nil;
_roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, 0.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
[_srcButton setHidden:NO];
}];
}];
NSArray * vertConstraint;
UIButton * button;
UITextField * textField;
-(void)search{
[self.view removeConstraints:vertConstraint];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(button, textField);
NSArray * newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-5-[textField(==30)]-(>=10)-[button]-10-|" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:newVertConstraint];
[textField resignFirstResponder];
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
vertConstraint = newVertConstraint;
}
self.automaticallyAdjustsScrollViewInsets = NO;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(search) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Search" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
textField = [[UITextField alloc] init];
textField.layer.borderWidth = 5;
textField.layer.borderColor = [UIColor redColor].CGColor;
[self.view addSubview:textField];
[textField setTranslatesAutoresizingMaskIntoConstraints: NO];
[button setTranslatesAutoresizingMaskIntoConstraints: NO];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(button, textField);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-20-[textField]-20-|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-20-[button]-20-|" options:0 metrics: 0 views:viewsDictionary]];
vertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-100-[textField(==30)]-10-[button]" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:vertConstraint];
[textField becomeFirstResponder];

UILabel height animation with autolayout

I have an UILabel on my view and UIButton. When button touched UILabel should change height animated, depends on label content . I was trying this:
- (void)viewDidLoad {
self.textLabel= [[UILabel alloc] initWithFrame:CGRectZero];
self.textLabel.numberOfLines=0;
self.textLabel.font= [UIFont systemFontOfSize:14];
self.textLabel.backgroundColor= [UIColor lightGrayColor];
self.textLabel.text= #"short text";
[self.view addSubview:self.textLabel];
[self.textLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[_textLabel]-10-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_textLabel)]];
self.button= [UIButton buttonWithType:UIButtonTypeSystem];
[self.button addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:#"Tap" forState:UIControlStateNormal];
self.button.backgroundColor= [UIColor greenColor];
[self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.button];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[_button]-10-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_button)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-50-[_textLabel(>=0)]-10-[_button(==20)]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_textLabel,_button)]];
}
- (void)buttonTouched:(id)buttonTouched {
self.shortText =!self.shortText;
self.textLabel.text= self.shortText ?#"short text":#"long long long text\nlong long long text\nlong long long text\n";
[UIView animateWithDuration:1.0
animations:^{
[self.view layoutIfNeeded];
}];
}
Right before the animation block, you need to call [self.view setNeedsUpdateConstraints] in order to trigger tell the view that the constraints need to be updated when you call layoutIfNeeded
So the new method:
- (void)buttonTouched:(id)buttonTouched {
self.shortText =!self.shortText;
self.textLabel.text= self.shortText ?#"short text":#"long long long text\nlong long long text\nlong long long text\n";
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:1.0
animations:^{
[self.view layoutIfNeeded];
}];
}
I have this example working no problem.
#interface ViewController ()
#end
#implementation ViewController
{
__weak UIView *_bgView;
__weak UILabel *_label;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *bgView = [[UIView alloc] initWithFrame:CGRectZero];
bgView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
bgView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bgView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[bgView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bgView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[bgView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bgView)]];
_bgView = bgView;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];
label.text = #"ahoj";
label.numberOfLines = 99;
label.textAlignment = NSTextAlignmentCenter;
label.translatesAutoresizingMaskIntoConstraints = NO;
[bgView addSubview:label];
[bgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-8-[label]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
[bgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-8-[label]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
_label = label;
[self performSelector:#selector(animate) withObject:nil afterDelay:1];
}
- (void)animate
{
_label.text = #"ahoj, ahoj\ntotalka ahoj";
[UIView animateWithDuration:1 animations:^{
[_bgView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
;
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Define the height constraint in your storyboard, add it as an IBOutlet, and then change its value inside your animation block.
Maybe if you use
[self.textLabel sizeToFit];
it works.

Resources