0

Hi i am creating button programatically. And i am working some app in ios6&ios7 4-inches screen and 3.5 inches screen. I need to display the button for all versions at same position.

So i want to use Auto layout concept for this.i know how to create button programatically, but I have no idea about how to apply auto layout concept for button. This is my code:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button addTarget:self action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];

[button setTitle:@"Show View" forState:UIControlStateNormal];

button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);

[view addSubview:button];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Surya
  • 151
  • 2
  • 14

2 Answers2

2

Try this,

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];

    [button setTitle:@"Show View" forState:UIControlStateNormal];

    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

    [self.view addConstraints:@[

                                //view1 constraints
                                [NSLayoutConstraint constraintWithItem:button
                                                             attribute:NSLayoutAttributeTop
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeTop
                                                            multiplier:1.0
                                                              constant:padding.top],

                                [NSLayoutConstraint constraintWithItem:button
                                                             attribute:NSLayoutAttributeLeft
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeLeft
                                                            multiplier:1.0
                                                              constant:padding.left],

                                [NSLayoutConstraint constraintWithItem:button
                                                             attribute:NSLayoutAttributeBottom
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeBottom
                                                            multiplier:1.0
                                                              constant:-padding.bottom],

                                [NSLayoutConstraint constraintWithItem:button
                                                             attribute:NSLayoutAttributeRight
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeRight
                                                            multiplier:1
                                                              constant:-padding.right],

                                ]];

    [self.view addSubview:button];

Update:

button.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin);

Ref.Link: Autoresizing masks programmatically vs Interface Builder / xib / nib

Community
  • 1
  • 1
Ramdhas
  • 1,765
  • 1
  • 18
  • 26
  • thanks for your response.If i create another button same procedure is going on? – Surya Apr 17 '14 at 11:40
  • bro it is working fine bro.But u are again updated answer na?so which one can i use it.And another question is if i create another button same procedure i will follow? – Surya Apr 17 '14 at 12:13
  • check both answer, use which one is perfect to u. And, you have to use this for all – Ramdhas Apr 17 '14 at 12:15
  • but for second you have to use suitable autoresizingmask, pls check attached link , you ll know – Ramdhas Apr 17 '14 at 12:19
  • If your question got satisfied answer, please upvote and accept. – Ramdhas Apr 17 '14 at 12:23
0

I hope this can help you Visit https://github.com/cloudkite/Masonry

warriorg
  • 86
  • 1
  • 5