Iphone 带有固定大小自定义图像的UIButton

Iphone 带有固定大小自定义图像的UIButton,iphone,objective-c,ios,ipad,uibutton,Iphone,Objective C,Ios,Ipad,Uibutton,我有一个自定义UIButton,如下所示: UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom]; [action setSize:CGSizeMake(30, 30)]; [action setBackgroundImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal]; [action

我有一个自定义UIButton,如下所示:

 UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom];
        [action setSize:CGSizeMake(30, 30)];
    [action setBackgroundImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal];
    [action setContentMode:UIViewContentModeCenter];
    [action addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
我拥有的真实图像的大小实际上是10x10,居中,我希望它保持这种方式,而不是缩放到按钮大小。我该怎么做

修订代码:

UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom];
    [action setSize:CGSizeMake(44, 44)];
    [action setImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal];
    [action addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    if (IS_IPAD){
        action.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        action.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }
     actionButton_ = [[UIBarButtonItem alloc] initWithCustomView:action];

它仍在缩放

可能会尝试这样的东西

 [button addSubview:imageView];

其中imageView包含图像

使用
ui按钮的
setImage:forState:
方法,而不是
setBackgroundImage:forState:


ui按钮
继承自
UIControl
,因此您可以将
内容水平对齐
内容垂直对齐
属性分别设置为
UIControl内容水平对齐中心
UIControl内容垂直对齐中心
(尽管在大多数情况下,这些已经是默认值)。

为此,我认为您需要使用图像插入属性使用setContentMode

[action setContentMode:UIViewContentModeCenter];

包括QuartzCore框架并改变层上的重力

action.layer.contentsGravity=kCAGravityCenter;

Swift 3的工作示例希望对您有所帮助

import UIKit

@IBDesignable
class MenuBtn: UIButton {

    convenience init() {
        self.init(frame: CGRect.zero)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }

    func setupView(){
        imageView?.contentMode = .scaleAspectFit
        var image = imageView?.image
        image = image?.withRenderingMode(.alwaysTemplate)

    }

    override var isSelected: Bool {
        didSet {
            tintColor = super.isSelected ? .gray : .gray
        }
    }

    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        let leftMargin:CGFloat = 40
        let imgWidth:CGFloat = 24
        let imgHeight:CGFloat = 24
        return CGRect(x: leftMargin, y: (contentRect.size.height-imgHeight) * 0.5, width: imgWidth, height: imgHeight)
    }

    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        let leftMargin:CGFloat = 80
        let rightMargin:CGFloat = 80
        return CGRect(x: leftMargin, y: 0, width: contentRect.size.width-leftMargin-rightMargin, height: contentRect.size.height)
    }

   override func backgroundRect(forBounds bounds: CGRect) -> CGRect {
       let leftMargin:CGFloat = 10
       let rightMargin:CGFloat = 10
       let topMargin:CGFloat = 10
       let bottomMargin:CGFloat = 10
       return CGRect(x: leftMargin, y: topMargin, width: bounds.size.width-leftMargin-rightMargin, height: bounds.size.height-topMargin-bottomMargin)
    }


    override func contentRect(forBounds bounds: CGRect) -> CGRect {
        let leftMargin:CGFloat = 5
        let rightMargin:CGFloat = 5
        let topMargin:CGFloat = 5
        let bottomMargin:CGFloat = 5
        return CGRect(x: leftMargin, y: topMargin, width: bounds.size.width-leftMargin-rightMargin, height: bounds.size.height-topMargin-bottomMargin)
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setupView()
    }

}

我在使用setImage时做了更改,如何设置ContentHorizontalAlignment它仍在缩放到UIButton大小