简体   繁体   中英

Resize xib UIView to fit UIButton inside

I have a UIView created from a xib file. Inside that UIView there is a UIButton with a text that may be longer or shorter depending on the language.

What I want is to resize the parent UIView to fit the width of the UIButton. How can I do it with AutoLayout, or programmatically?

Thanks,

add leading, trailing, top and bottom constraint of button

add width constraint of view and connect to viewWidthConstraint.

calculate width of button for language and set constant of viewWidthConstraint.

View and button will resize

@property (nonatomic, weak) IBOutlet NSLayoutConstraints *viewWidthConstraint;

- (void)viewDidLoad {
  super.viewDidLoad();

  self.viewWidthConstraint.constant = [self buttonWidth];
}

- (CGFloat)buttonWidth {
  return 100.f; (calculate width of button)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM