MaSza
MaSza

Reputation: 465

How to get button size before view is load?

I looking for some solution how to get size of button before view is load. I want to make button round so I try this

-(void) viewWillAppear:(BOOL)animated{
    CGSize loopButtonSize= _loopButton.frame.size;
    _loopButton.layer.cornerRadius=loopButtonSize.height/2.0f;
    NSLog(@"viewWillAppear button height: %f", loopButtonSize.height);
}

But this show wrong button size (1000 instead of for example 43) My button has dynamic change size so I can't use constant size values.

This works for me but it change button shape after view loading. So if I use animated load then for second appear square button which then change to round.

-(void)viewDidAppear:(BOOL)animated{
    CGSize loopButtonSize= _loopButton.frame.size;
    _loopButton.layer.cornerRadius=loopButtonSize.height/2.0f;

    NSLog(@"viewDidAppear button height: %f", loopButtonSize.height);

}

Any ideas how make it correctly?

Upvotes: 1

Views: 308

Answers (2)

Duncan C
Duncan C

Reputation: 131418

Put that code in your viewDidLayoutSubviews method.

Upvotes: 1

Aakash
Aakash

Reputation: 2269

Write the code to round the button in viewDidLayoutSubviews method, since it is called just after the view is laid out you will get your button properly rounded.

Upvotes: 1

Related Questions