मैं इस पुराने धागे का जवाब दे रहा हूं क्योंकि यह इस समस्या के समाधान के लिए निरंतर खोज में आता है और मैंने कहीं और समाधान नहीं देखा है। यह वास्तव में कष्टप्रद है कि setTintColor केवल UIButton की हाइलाइट की गई स्थिति पर लागू होता है। छह महीने पहले, यह उतना ही परेशान था कि यह केवल आईओएस 5 पर लागू होता था, लेकिन उम्मीद है कि आगे आने वाली कोई समस्या कम होगी। इस बात को ध्यान में रखते हुए, मैंने अपने सामान्य राज्य में बटनों के समूह को टिन करने के लिए एक सामान्य उद्देश्य समाधान को मिश्रित करने के लिए कई समुदाय सुझावों को तैयार किया है और संयुक्त किया है।
नीचे दी गई विधि UIButtons के एनएसएआरएआरई और इनपुट के रूप में रंग विनिर्देशों का एक सेट स्वीकार करती है। यह setTintColor का उपयोग करके एक बटन पर रंग विनिर्देशों को लागू करता है, परिणाम को UIImage में प्रस्तुत करता है, और उस छवि को बटन के पूरे सेट की पृष्ठभूमि छवि के रूप में लागू करता है। यह बटन रंगों के लिए अलग छवि फ़ाइलों को बनाने की आवश्यकता से बचाता है। साथ ही, यह एक खिंचाव वाली छवि का उपयोग करके ऐसा करता है ताकि यह विभिन्न आकारों के बटनों के संग्रह के साथ काम कर सके (हालांकि ध्यान दें कि यह UIButton के डिफ़ॉल्ट कोने राउंडिंग कारकों को मानता है)। मुझे आशा है कि आपको आईओएस 5 लक्ष्यों के लिए यह उपयोगी लगेगा।
- (void) setColorOfButtons:(NSArray*)buttons red:(float)red green:(float)green blue:(float)blue alpha:(float)alpha {
if (buttons.count == 0) {
return;
}
// get the first button
NSEnumerator* buttonEnum = [buttons objectEnumerator];
UIButton* button = (UIButton*)[buttonEnum nextObject];
// set the button's highlight color
[button setTintColor:[UIColor colorWithRed:red/255.9999f green:green/255.9999f blue:blue/255.9999f alpha:alpha]];
// clear any existing background image
[button setBackgroundImage:nil forState:UIControlStateNormal];
// place the button into highlighted state with no title
BOOL wasHighlighted = button.highlighted;
NSString* savedTitle = [button titleForState:UIControlStateNormal];
[button setTitle:nil forState:UIControlStateNormal];
[button setHighlighted:YES];
// render the highlighted state of the button into an image
UIGraphicsBeginImageContext(button.layer.frame.size);
CGContextRef graphicsContext = UIGraphicsGetCurrentContext();
[button.layer renderInContext:graphicsContext];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIImage* stretchableImage = [image stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIGraphicsEndImageContext();
// restore the button's state and title
[button setHighlighted:wasHighlighted];
[button setTitle:savedTitle forState:UIControlStateNormal];
// set background image of all buttons
do {
[button setBackgroundImage:stretchableImage forState:UIControlStateNormal];
} while (button = (UIButton*)[buttonEnum nextObject]);
}
स्रोत
2012-02-18 11:21:43
यह काम नहीं करता है, मुझे 'UIButton' '-setTintColor:' 'UIButton' का जवाब नहीं दे सकता है 'setTintColor:' – Syntax
आप सही हैं, मैंने फिर से जांच की है, और यह केवल आईओएस के लिए काम करता है 5.0 और बाद में। इस मामले में, आपको एक UIView बनाना होगा, इसके पृष्ठभूमि रंग को सेट करना होगा, और हाइलाइट किए गए राज्य के लिए पृष्ठभूमि दृश्य के रूप में इस दृश्य का उपयोग करना होगा। – antalkerekes
ठीक है, मैं आईओएस 5 को एक जाऊंगा। – Syntax