2012-03-14 13 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]; 

वास्तविक छवि मेरे पास है कि वास्तव में, 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]; 

जो अभी भी स्केलिंग है

उत्तर

2
इस मुझे लगता है कि आप छवि का उपयोग करने की जरूरत है के लिए

संपत्ति UIButton Class Reference

1

हो सकता है इस liky कुछ कोशिश ...

[button addSubview:imageView]; 

जहां imageView छवि होता है; आप UIControlContentHorizontalAlignmentCenter और UIControlContentVerticalAlignmentCenter क्रमशः contentHorizontalAlignment और contentVerticalAlignment गुण निर्धारित कर सकते हैं (हालांकि ज्यादातर मामलों में इन पहले से ही मूलभूत मूल्यों कर रहे हैं)

2

UIControl से UIButtonsetBackgroundImage:forState: के बजाय

UIButton inherits की setImage:forState: विधि का उपयोग करें।

+0

मैं setImage का उपयोग कर, मैं कैसे contentHorizontalAlignment – adit

+0

यह अभी भी UIButton आकार करने के लिए स्केलिंग है स्थापित कर सकता में बदल गया है – adit

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

}