2

I am little bit new at AutoLayout.I know already tons of question and tutorial available regarding this autoLayout but I have not found my solution.So Thanks in advance for any help.

What is my requirement?

I have to make UIView which will come to screen after pressing a button from the bottom side of the screen with animation.(Like Keyboard).I have made this UIView in a xib file using autoLayout.So far i have done like this.

In ViewDidLoad:

//Loading custom UIView 
containerView = [[SharingPickerView alloc] init];
[containerView loadingMenus:pickerData];
[self.view addSubview:containerView];

In this view it contatains (a scrollview then a page controller then a cancel button)

In ViewWillLayoutSubviews:

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];

//Changing the frame of view to bottom side of the screen so that we can animate it later from bottom to top later.

containerView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, containerView.frame.size.height);
[containerView setBackgroundColor:[[UIColor colorWithRed:231.0/255.0f green:231.0/255.0f blue:231.0/255.0f alpha:1.0f] colorWithAlphaComponent:0.3]];
}

Now on press for animation.

-(void)sharePickerView{
   [UIView animateWithDuration:1 animations:^(){
   containerView.frame = CGRectMake(0,self.view.frame.size.height-containerView.frame.size.height, self.view.frame.size.width, containerView.frame.size.width);
   } completion:^(BOOL isFinished){
  NSLog(@"Animation Finish");
   }];
}

Here i am re-framing the View for showing that animation but some bottom portion(cancel button of that view) is not showing in iPhone 6 simulator but it is showing perfectly iPhone 5s device.

Regards Emon.

Emon
  • 452
  • 3
  • 11
  • 21
  • Here's a tutorial on how you can do static autolayout animations easily https://www.youtube.com/watch?v=8KVKXlh6sKI – Arjay Waran Nov 15 '15 at 22:08

4 Answers4

1

try to use size constraints as outlets. Then you can easy change values of those constraints. It's not really recommended to modify frame when using auto-layout.

declare a property:

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;

and in the implementation:

heightConstraint = self.view.frame.size.height;

Also this post may be helpful: AutoLayout, Constraints and Animation

Community
  • 1
  • 1
Mobile Developer
  • 5,730
  • 1
  • 39
  • 45
1

You shouldn't reframe if you are using NSAutoLayout.

  1. You have to install a constraint at bottom of the screen with the scroll and the view parent. Remember set translatesAutoresizingMaskIntoConstraints to NO when you instance the custom view.

enter image description here

  1. Create an Outlet of this constraint.
  2. When you need change animating

    _ctScrollBottom.constant = self.view.frame.size.height-containerView.frame.size.height;
    
    [UIView animateWithDuration:animationDuration
                     animations:^(){
                         [self.view  layoutIfNeeded];
                     }];
    

I took your "y" position calculation but I have not tested it ;)

Hope this helps!

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Carlos Jiménez
  • 527
  • 5
  • 15
0

i suggest you to add constraints programmatically

  1. First, set your translatesAutoresizingMaskIntoConstraints to NO

    containerView = [[SharingPickerView alloc] init];
    containerView.translatesAutoresizingMaskIntoConstraints = NO;
    [containerView loadingMenus:pickerData];
    [self.view addSubview:containerView];
    
  2. second, add constraints for height

    // for example, half height as view.
    
    self.heightConstraint = [NSLayoutConstraint
          constraintWithItem:containerView
                   attribute:NSLayoutAttributeHeight
                   relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                   attribute:NSLayoutAttributeHeight
                  multiplier:0.5
                    constant:0.0]
    [self.view addConstraint:self.heightConstraint];`
    

Hold this constraint as property:

@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;

Animate constraint changes as

[UIView animateWithDuration:0.5
                 animations:^{
                     self.heightConstraint.constant += Value_to_change;
                     [self.view layoutIfNeeded];
                 }];

Also, do not forget to add trailing and leading constraints + top constraint

Here is good tutorial for you http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/

Hope this helps

Ell Neal
  • 6,014
  • 2
  • 29
  • 54
Doro
  • 2,413
  • 2
  • 14
  • 26
0

This answer https://stackoverflow.com/a/26040569/2654425 may be very helpul as it deals with a similar situation.

Community
  • 1
  • 1
DevC
  • 6,982
  • 9
  • 45
  • 80