2012-03-14 131 views
5

我有一個自定義的UIButton如下:的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]; 

,我有真正的圖像實際上是大小爲10×10,居中,我希望它保持下去,而不是縮放到按鈕大小。我怎麼做?

修改後的代碼:

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]; 

這仍然是縮放

回答

1

可能嘗試一些liky這...

[button addSubview:imageView]; 

其中的ImageView包含圖像;

2

UIControl使用的UIButton代替setBackgroundImage:forState:

UIButton繼承setImage:forState:方法,這樣你就可以設置contentHorizontalAlignmentcontentVerticalAlignment屬性UIControlContentHorizontalAlignmentCenterUIControlContentVerticalAlignmentCenter,分別爲(雖然在大多數情況下,這些已經是默認值)。

+0

我已經在用setImage,我怎麼設置contentHorizo​​ntalAlignment – adit 2012-03-14 23:01:53

+0

它仍然是縮放到UIButton的大小改變 – adit 2012-03-14 23:15:28

0

使用setContentMode

[action setContentMode:UIViewContentModeCenter]; 
0

包括QuartzCore框架和更改層上的重力。

action.layer.contentsGravity=kCAGravityCenter; 
1

例如工作雨燕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() 
    } 

}