3

I have to show a popover on iPhone screen with multiple "Switch" controls. And to add and remove subviews on/from popover view with switch on/off actions respectively. For better illustration of the situation see below

images.Initial popover

The above popover view first appears when user taps on a button. The popover has to stay always at the center of the screen and initially add contact switch will be in off condition. When turned on the below subviews has to be added on popover while keeping the popover in center of the screen and increasing the height of popover as per subviews. Add contact switch "ON"

And just like the above the popover view has to grow again in height with adding two more subviews when "Add mail" switch will be "ON". And finally look like this,

Final popoverView

That's it. I am using auto-layout through out my application and this is where I am perplexed. I know I can remove the popovers and one more new each time but that seems to be kind of novice option. So is there any simple way to add subviews and expand its superview dynamically with auto-layout ? I've seen many questions with UILabel and working with respect to it's intrinsic content size but still unable to get any idea with this particular situation. Any help will be appreciated. Happy coding.

Community
  • 1
  • 1
Rameswar Prasad
  • 1,331
  • 17
  • 35
  • What do your constraints look like in the original case? – Andrew Monshizadeh Dec 05 '14 at 13:44
  • Currently I haven't set any constraints as I am not able to get clear idea actually how to do it, as you see I have to add subviews dynamically so I've not idea how to do it with auto-layout. As it says even if I hide the subviews then also it's constraints will come into play while calculating the subviews. – Rameswar Prasad Dec 05 '14 at 13:58
  • please review my answer below for how you can use layout constraints to accomplish what you are after. – Andrew Monshizadeh Dec 06 '14 at 18:04

2 Answers2

4

This can be accomplished with plain layout constraints without having to manually constrain the height of the container view, and then update the constant of that constraint.

The way to do this, is to constrain the container view's height based on the bottom of the bottom most subview.

conatiner constraints

Then put a reference to this constraint within your view controller.

constraint reference

now you can write something like the following view controller, which will add a new subview at the bottom of the container view, and automatically update the container view's height.

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (nonatomic, weak) UIView *lastView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.lastView = self.addButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addButtonTapped:(id)sender {
    UIView *newView = [[UIView alloc] initWithFrame:CGRectZero];
    newView.translatesAutoresizingMaskIntoConstraints = NO;
    newView.backgroundColor = [UIColor redColor];
    [newView addConstraint:[NSLayoutConstraint constraintWithItem:newView
                                                       attribute:NSLayoutAttributeHeight
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:nil
                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                      multiplier:1.0
                                                         constant:35]];

    [self.containerView addSubview:newView];
    [self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lastView]-(14)-[newView]"
                                                                     options:NSLayoutFormatAlignAllCenterX
                                                                     metrics:nil
                                                                        views:@{@"lastView" : self.lastView, @"newView" : newView}]];
    [self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[newView]-(10)-|"
                                                                     options:NSLayoutFormatAlignmentMask
                                                                     metrics:nil
                                                                        views:@{@"newView":newView}]];

    [self.containerView removeConstraint:self.bottomConstraint];
    self.bottomConstraint = [NSLayoutConstraint constraintWithItem:self.containerView
                                                         attribute:NSLayoutAttributeBottom
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:newView
                                                         attribute:NSLayoutAttributeBottom
                                                        multiplier:1.0
                                                          constant:14];
    [self.containerView addConstraint:self.bottomConstraint];

    self.lastView = newView;
}
@end

Add this all together, and you should get the following behavior.

enter image description here

Andrew Monshizadeh
  • 1,784
  • 11
  • 16
0

You can outlet height constraint of the view, and then set value accordingly to elements.

mokiSRB
  • 1,132
  • 7
  • 16
  • Do you mean I have to take height constraint IBOutlets for all subviews and also the container popover view and change them dynamically as first keeping the constraints of the subviews as "Zero" which are not required and then when need to be shown, change them and also change the container Views height constraint with some little expanding animation ? – Rameswar Prasad Dec 05 '14 at 14:01
  • Yes. You do not need 3 subviews, you can do it only with one. Hide textfield, then action will textField.hidden = NO and set _heightCOnstraint = 60.00; – mokiSRB Dec 05 '14 at 14:55