调整动画大小后,UIButton不尊重Aspect Fill contentMode [英] UIButton not respect Aspect Fill contentMode after resizing animation

查看:574
本文介绍了调整动画大小后,UIButton不尊重Aspect Fill contentMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自动布局。以下是视图的初始状态。

I am use auto layout. Following is the initial state of the view.

中间是视图中包含的按钮。该按钮具有contentMode Aspect Fill,图像被设置为按钮的背景图像。

In the centre is a button contained in a view. The button has contentMode Aspect Fill, and the image is set as the background image of the button.

然后我使用以下代码转换视图,这将放大中心卡以填充屏幕,并将图像移动到视图的顶部:

Then I use the following code to transform the view, which will enlarge the centre card to fill the screen, and move the image to the top of the view:

cardTrailingSpaceConstraint.constant = 0
cardLeadingSpaceConstraint.constant = 0
cardView.removeConstraint(cardAspectRatioConstraint)
let cardHeightConstraint = NSLayoutConstraint(item: cardView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1.0, constant: 0)
view.addConstraint(cardHeightConstraint)

dishImageButton.removeConstraint(dishButtonBottomSpaceConstraint)
let dishButtonHeightConstraint = NSLayoutConstraint(item: dishImageButton, attribute: .Height, relatedBy: .Equal, toItem: cardView, attribute: .Height, multiplier: 0.2, constant: 0)
cardView.addConstraint(dishButtonHeightConstraint)

cardView.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: nil, animations: { [unowned self] () -> Void in
    self.cardHeader.alpha = 0
    self.cardView.layer.cornerRadius = 0
    self.cardView.layoutIfNeeded()

    }) { [unowned self] (finished) -> Void in

        }

结果是:

然而,这不是我想要的。该按钮不符合contentMode,然后图像被拉伸。

However, it is not what I want. The button is not respecting the contentMode and the image then get stretched.

有人能告诉我如何维护按钮的Aspect Fill contentMode吗?

Can anyone tell me how to maintain the Aspect Fill contentMode of the button?

推荐答案


  1. 将按钮类型设置为 UIButtonTypeCustom (故事板或xib中的自定义)。

  2. 设置按钮的图像,而不是按钮的背景图像。

  3. In viewDidLoad ,将 button.imageView.contentMode 设置为 UIViewContentMode.ScaleAspectFill

  1. Set the button type to UIButtonTypeCustom ("Custom" in a storyboard or xib).
  2. Set the button's image, not the button's background image.
  3. In viewDidLoad, set button.imageView.contentMode to UIViewContentMode.ScaleAspectFill.

这篇关于调整动画大小后,UIButton不尊重Aspect Fill contentMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆