简体   繁体   中英

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.

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)]"
  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.

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