2012-05-23 13 views
5

UIBarbuttonItem का उपयोग करके, initWithImage मुझे एक तस्वीर मिलती है जिसे मैं छोटा चाहता हूं।UIBarButtonItem छवि आकार को बदलने के लिए कैसे करें

मुझे लगता है कि छवि का आकार बदलने का बिल्कुल कोई तरीका नहीं है।

UIedgeInsetMake बिल्कुल संचालित नहीं होता है। पिकुट्रे का आकार बदलना या तो (पिक्सेललेट) संचालित नहीं करता है। मेरे पास @ 2x 48x48 आइकन और सामान्य 24x24 है। बड़ी खाली सीमा के साथ एक नई तस्वीर बनाना काम नहीं करता है।

यदि मैं 20x20 का उपयोग करता हूं तो यह पिक्सेललेट होगा। कोई बात नहीं क्या।

कोई समाधान? धन्यवाद!

उत्तर

1

यदि आप छवि से मेल खाने के लिए बटन का आकार बदलना चाहते हैं तो UIButton बनाने और UIBarButtonItem कस्टम बनाने के लिए बेहतर हो सकता है। UIBarButtonItem initWithImage में छवि UIBarButtonItem फिट करने के लिए स्केल की गई है।

this उत्तरदाता इसे कैसे करें इसके बारे में अधिक जानकारी के लिए देखें।

+0

आपके उत्तर के लिए धन्यवाद, लेकिन बटन आकार ठीक है, वास्तव में यह एकदम सही है। यह अंदर खींची गई छवि है जिसे मैं छोटा चाहता हूं। –

3

आप छवि सेट और bellow विधि का उपयोग कर शीर्षक आकार के साथ आकार को समायोजित करने के लिए कस्टम BarbuttonItem उपयोग कर सकते हैं:

+(UIBarButtonItem *)createToolBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a 
{ 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
// Since the buttons can be any width we use a thin image with a stretchable center point 
UIImage *buttonImage = [[UIImage imageNamed:@"toolbarbutton_button_mouseup.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0]; 
UIImage *buttonPressedImage = [[UIImage imageNamed:@"toolbarbutton_button_mouseover.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0]; 
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0f]]; 
//[[button titleLabel] setFont:[UIFont fontWithName:@"Futura-Medium" size:12.0]]; 
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; 
[[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)]; 

CGRect buttonFrame = [button frame]; 
buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0; 

//Applicable only for iPhone FrameFun 
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && [[[NSUserDefaults standardUserDefaults] stringForKey:@"is_Landscape"] isEqualToString:@"landscape"]) { 
    buttonFrame.size.height = 23.0; 
}else { 

    buttonFrame.size.height = buttonImage.size.height; 
} 

[button setFrame:buttonFrame]; 

[button setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted]; 

[button setTitle:t forState:UIControlStateNormal]; 

[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
return [buttonItem autorelease]; 

} 

यह विधि आपको इच्छा छवि के साथ बटन के गतिशील आकार बनाने के लिए करते हैं। यहां मैंने छवि समायोजित करने के लिए stretchableImageWithLeftCapWidth का उपयोग किया है। मुझे लगता है कि यह आपकी मदद करेगा। आप कस्टम बारबटन को भी बनाने के लिए पूरी विधि का उपयोग कर सकते हैं।

+0

अच्छा कोड लेकिन यह वह नहीं है जिसे मैं ढूंढ रहा हूं। मैं कस्टम बटन "ड्रा" नहीं करना चाहता हूं। मैं एक क्लासिक barbuttonItem एक तस्वीर के साथ डिफ़ॉल्ट रूप से अंदर खींचा गया है की तुलना में थोड़ा सा छोटा अंदर चाहता हूँ। –

5

यह, निम्नलिखित द्वारा प्राप्त किया जा सकता

  1. खुला आकार UIBarbuttonItem
  2. परिवर्तन की निरीक्षक "बार आइटम" के लिए मान -> छवि इनसेट -> ऊपर/नीचे/छोड़ दिया/सही।

इसे आज़मा कर देखें ...

+0

यह मेरे लिए पूरी तरह से काम करता है। खासकर, पीडीएफ छवियों के लिए उपयोगी –

संबंधित मुद्दे