2

I used this code(in UIView class).

- (void)setup {

    self.backgroundColor = [UIColor colorWithWhite:0.12f alpha:1.0f];

    _doneTextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _doneTextButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_doneTextButton setTitle:@"Save"  forState:UIControlStateNormal];
    [_doneTextButton setTitleColor:CODE_A_COLOR forState:UIControlStateNormal];

     [_doneTextButton setBackgroundColor:CODE_6_COLOR] ;
    [_doneTextButton.titleLabel setFont:[FontManager getOpenSansBoldFontWithSize:16]];
    [_doneTextButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:_doneTextButton];
}

I am getting this output(buttons at corner).

enter image description here

I want this "Cancel" and "Save" button exactly fit equally in UIView

enter image description here

I am using third party lib. and no idea straight logic not working.How do i set frame of both buttons? expecting autoLayout code. thanks in advance.

Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68

4 Answers4

2

you can set the frame of the buttons to self.view.frame.size.width/2 and then you can set the second buttons frame by taking the end points of the first button

-(void)setup
{
btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];

btnSave.frame = CGRectMake(0, self.view.frame.size.height-30, self.view.frame.size.width/2, 30);

btnCancel.frame = CGRectMake(btnSave.frame.origin.x+btnSave.frame.size.width+5,  self.view.frame.size.height-30, btnSave.frame.size.width, 30);

btnSave.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[btnSave setTitle:@"Save"  forState:UIControlStateNormal];
[btnSave setBackgroundColor:[UIColor blackColor]] ;
[self.view addSubview:btnSave];

btnCancel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[btnCancel setTitle:@"Cancel"  forState:UIControlStateNormal];
[btnCancel setBackgroundColor:[UIColor blackColor]] ;
[self.view addSubview:btnCancel];

}
Rahul Patel
  • 1,822
  • 12
  • 21
1

I suppose you are using autolayout in your Storyboard, so you have to use the layout constraints to achieve this behavior.

Create the button with frame equal to CGRectZero and then, in the updateViewConstraints method you can create the constraints necessary to setup the buttons' positions. Here is an example:

-(void)updateViewConstraints {
    [super updateViewConstraints];
    if(!constraintsAdded) {
        [button1 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [button2 setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button1
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:button2
                                                          attribute:NSLayoutAttributeWidth
                                                         multiplier:1.0
                                                           constant:0.0]];
        constraintsAdded = true;
    }
}
Zeb
  • 1,715
  • 22
  • 34
0

Please use this solution if you're not going with autolayout.

It seems you are going to set width with your view's frame like self.view.frame.size.width. In some cases it will return same size which size you defined in xib or storyboard. So that's why its happening, for more info please have a look

UIViewController viewDidLoad incorrect width/height

Please use this solution to get rid such issues.

float width  = [UIScreen mainScreen].bounds.size.width;

self.cancelButton.frame = CGRectMake(0, self.view.frame.size.height+self.view.frame.origin.y-40, width/2, 40);

self.doneButton.frame = CGRectMake(self.cancelButton.frame.size.width+self.cancelButton.frame.origin.x+1, self.view.frame.size.height+self.view.frame.origin.y-40, width/2, 40);
Community
  • 1
  • 1
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Using UIStackView.and add the two button in the stackview.

Scott
  • 162
  • 1
  • 12