简体   繁体   English

UIImageView和代码中的自动布局

[英]UIImageView and Auto Layout in Code

I'm trying to use programmatic visual constraints to display a label and a button next to one another. 我正在尝试使用程序化视觉约束来彼此相邻显示标签和按钮。 However, the UIImageView used as the button's background is making the intrinsic size of the button much too large. 但是,用作按钮背景的UIImageView会使按钮的固有尺寸太大。

I attempted to add a constraint that forces the height of the button to match the height of the label. 我试图添加一个约束,以强制按钮的高度与标签的高度匹配。 But I just got a super tall label instead of a smaller button. 但是我只是得到了一个超高的标签,而不是一个较小的按钮。

  1. How do I set a constraint so that the button height is the same height as the label (and not vice-versa) 如何设置约束,以使按钮的高度与标签的高度相同(反之亦然)
  2. The button should keep the original aspect ratio of the image - its width should also match its own height (maybe this comes for free?) 该按钮应保持图像的原始长宽比-其宽度也应与其自身的高度匹配(也许这是免费提供的?)

The following works for this: 为此,以下工作:

  1. Set a width for the button in the visual layout: @"|-[titleLabel][refreshButton(==26)]" 在视觉布局中设置按钮的宽度: @"|-[titleLabel][refreshButton(==26)]"
  2. Add a constraint such that the height of the button is equal to its own (now explicit) width: 添加一个约束,使按钮的高度等于其自身的宽度(现在是显式的):

      NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:refreshButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:refreshButton attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]; 

I would still prefer a solution that uses the label's height, instead of a fixed value. 我仍然希望使用标签高度而不是固定值的解决方案。

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

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