2012-03-14 14 views
5

ho un UIButton personalizzato come segue:UIButton immagine personalizzata dimensione fissa con

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

La vera immagine che ho è in realtà una dimensione di 10x10, centrato e voglio che rimanga in questo modo e non in scala al dimensione del pulsante. Come lo faccio?

codice rivisto:

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

che è ancora scalabilità orizzontale

risposta

1

Può essere provare qualcosa Liky questo ...

[button addSubview:imageView]; 

dove IMAGEVIEW contiene l'immagine;

2

usare il metodo UIButton anziché setBackgroundImage:forState:

UIButton eredita setImage:forState: da UIControl, in modo da poter impostare le proprietà contentHorizontalAlignment e contentVerticalAlignment rispettivamente UIControlContentHorizontalAlignmentCenter e UIControlContentVerticalAlignmentCenter, (sebbene nella maggior parte dei casi questi sono già i valori di default).

+0

ho cambiato nell'uso del setImage, come faccio a impostare il contentHorizontalAlignment – adit

+0

E 'ancora scalata al formato UIButton – adit

0

uso setContentMode

[action setContentMode:UIViewContentModeCenter]; 
0

Includere quadro QuartzCore e cambiare la gravità sul livello.

action.layer.contentsGravity=kCAGravityCenter; 
1

esempio Lavorare per Swift 3 Speranza che aiuta

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() 
    } 

} 
Problemi correlati