调整动画大小后,UIButton不尊重Aspect Fill contentMode

Han*_*nXu 24 ios swift ios8

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

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

在此输入图像描述

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

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

        }
Run Code Online (Sandbox Code Playgroud)

结果是:

在此输入图像描述

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

任何人都可以告诉我如何维护按钮的Aspect Fill contentMode?

rob*_*off 45

  1. 将按钮类型设置为UIButtonTypeCustom(故事板或xib中的"自定义").
  2. 设置按钮的图像,而不是按钮的背景图像.
  3. viewDidLoad,设置button.imageView.contentModeUIViewContentMode.ScaleAspectFill.


小智 13

斯威夫特3


离开Rob的回答:

    let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 30))
    btn.setImage(UIImage(named: "albumsBtn"), for: UIControlState.normal)
    btn.imageView?.contentMode = UIViewContentMode.scaleAspectFit
    btn.addTarget(self.navigationController, action: #selector(CustomGalleryViewController.showAlbums(_:)), for:  UIControlEvents.touchUpInside)
    let item = UIBarButtonItem(customView: btn)
    self.navigationItem.leftBarButtonItem = item
Run Code Online (Sandbox Code Playgroud)


Vik*_*wal 6

斯威夫特2

button.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
Run Code Online (Sandbox Code Playgroud)

所有contentMode:

  .ScaleToFill
   .ScaleAspectFit
   .ScaleAspectFill
   .Redraw 
   .Center 
   .Top
   .Bottom
   .Left
   .Right
   .TopLeft
   .TopRight
   .BottomLeft
   .BottomRight
Run Code Online (Sandbox Code Playgroud)


Yar*_*aro 6

[btn setImage:forState:]用法之前尝试以下操作:

    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)
  • 斯威夫特4
//Center
btn.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.center

//Left
btn.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left

//Right
btn.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right
Run Code Online (Sandbox Code Playgroud)


bra*_*tky 6

在Xcode 10.1中,您可以使用界面生成器。在右侧的属性检查器中,使用“控制”部分,如下所示:

iOS Swift XCode 10.1 uibutton缩放以填充


Ram*_*ami 6

迅速

对于按钮有 contentMode Aspect Fill:

btn.contentHorizontalAlignment = .fill
btn.contentVerticalAlignment = .fill
btn.imageView?.contentMode = .scaleAspectFill
Run Code Online (Sandbox Code Playgroud)


xsc*_*der 5

Swift 2.x版本:

let myButton = UIButton(type: .Custom)
myButton.frame = CGRectMake(0, 0, 200, 20)
myButton.backgroundColor = UIColor.blackColor()
myButton.imageView.contentMode = .ScaleAspectFill // ALTERNATIVE:  .ScaleAspectFit
myButton.setImage(UIImage(named: "myImageName"), forState: .Normal)
myButton.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
view.addSubview(myButton)
Run Code Online (Sandbox Code Playgroud)

  • 确实!对我来说,直到我制作了类型自定义它才起作用。谢谢。 (2认同)

Nik*_*Kov 5

斯威夫特4.2

延期

// Inspectable image content mode
extension UIButton {
    /// 0 => .ScaleToFill
    /// 1 => .ScaleAspectFit
    /// 2 => .ScaleAspectFill
    @IBInspectable
    var imageContentMode: Int {
        get {
            return self.imageView?.contentMode.rawValue ?? 0
        }
        set {
            if let mode = UIViewContentMode(rawValue: newValue),
                self.imageView != nil {
                self.imageView?.contentMode = mode
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:此和其他答案不适用于ios11。最接近的答案是@Jaro,但我认为最好制作一个UIImageView并在其上添加一个按钮,或者创建一个自定义类的UIImageView手势识别器,然后单击动画。


小智 5

语言似乎有了更新。

我不得不深入挖掘以获得 xcoder 解决方案:

myButton.imageView.contentMode = .ScaleAspectFill 
Run Code Online (Sandbox Code Playgroud)

更新后的版本如下:

myButton.imageView?.contentMode = UIView.ContentMode.scaleAspectFit