简体   繁体   English

调整xib UIView的大小以适合其中的UIButton

[英]Resize xib UIView to fit UIButton inside

I have a UIView created from a xib file. 我有一个从xib文件创建的UIView。 Inside that UIView there is a UIButton with a text that may be longer or shorter depending on the language. 在该UIView中,有一个UIButton,其文本根据语言的不同可能更长或更短。

What I want is to resize the parent UIView to fit the width of the UIButton. 我想要的是调整父UIView的大小以适合UIButton的宽度。 How can I do it with AutoLayout, or programmatically? 如何使用AutoLayout或以编程方式实现?

Thanks, 谢谢,

add leading, trailing, top and bottom constraint of button 添加按钮的前导,尾随,顶部和底部约束

add width constraint of view and connect to viewWidthConstraint. 添加视图的宽度约束并连接到viewWidthConstraint。

calculate width of button for language and set constant of viewWidthConstraint. 计算语言的按钮宽度并设置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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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