我使用自动布局.以下是视图的初始状态.
中间是视图中包含的按钮.该按钮具有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
UIButtonTypeCustom
(故事板或xib中的"自定义").viewDidLoad
,设置button.imageView.contentMode
为UIViewContentMode.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)
斯威夫特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)
在[btn setImage:forState:]用法之前尝试以下操作:
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)
//Center
btn.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.center
//Left
btn.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
//Right
btn.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right
Run Code Online (Sandbox Code Playgroud)
迅速
对于按钮有 contentMode Aspect Fill:
btn.contentHorizontalAlignment = .fill
btn.contentVerticalAlignment = .fill
btn.imageView?.contentMode = .scaleAspectFill
Run Code Online (Sandbox Code Playgroud)
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)
延期
// 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
归档时间: |
|
查看次数: |
29836 次 |
最近记录: |