2014-05-11 11 views
15

जब मैं UIView को एक व्यू कंट्रोलर प्रोग्रामेटिक रूप से जोड़ता हूं, तो मैं पृष्ठभूमि रंग को किसी अन्य रंग में बदलने में सक्षम नहीं हूं, यह हमेशा काला रहता है।UIView पृष्ठभूमि रंग हमेशा काला

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    self.backgroundColor = [UIColor blueColor]; 

    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    [path moveToPoint:CGPointMake(100, 33)]; 
    [path addLineToPoint:CGPointMake(200, 33)]; 
    path.lineWidth = 5; 
    [[UIColor redColor] setStroke]; 
    [path stroke]; 
} 

जब मैं drawrect: बाहर टिप्पणी और रंग में परिवर्तन के प्रारंभकर्ता को self.backgroundColor = [UIColor blueColor]; जोड़ें:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.backgroundColor = [UIColor blueColor] 
    } 
    return self; 
} 

ऐसा क्यों है और क्या मैं बदलने की जरूरत है? दरअसल मैं पृष्ठभूमि को पारदर्शी होना चाहता हूं।

उत्तर

13

आपके विचार के backgroundColor अगर आप drawRect का उपयोग कर अपने आप को देखने आकर्षित नजरअंदाज कर दिया है।

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    [[UIColor blueColor] setFill]; // changes are here 
    UIRectFill(rect);    // and here 

    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    [path moveToPoint:CGPointMake(100, 33)]; 
    [path addLineToPoint:CGPointMake(200, 33)]; 
    path.lineWidth = 5; 
    [[UIColor redColor] setStroke]; 
    [path stroke]; 
} 
+4

जब मैं पृष्ठभूमि रंग चाहता हूं तो उत्तर पूरी तरह से काम करता है। लेकिन मैं [[UIColor clearColor] setFill] का उपयोग नहीं कर सकता; यह अभी भी काला आता है। – Pete

+3

@Pete, मैं उसी परिणाम को देखता हूं जब आप drawRect में रंग को सेट करने का प्रयास करते समय करते हैं। इसे ठीक करने के लिए, उस यूआईआरईटीफ़िल कॉल को हटाएं और दृश्य की init विधि में स्पष्ट करने के लिए पृष्ठभूमि रंग सेट करें (जो कभी भी विधि को आपके द्वारा दृश्य बनाने के तरीके के लिए उपयुक्त है)। – rdelmar

+1

@Pete कृपया इस प्रश्न को जांचें http://stackoverflow.com/questions/2125543/setting-a-cgcontext-transparent-background।शायद यह – Avt

21

जैसा कि आप drawRect: लागू कर रहे हैं, यह UIView के किसी भी डिफ़ॉल्ट ड्राइंग को ओवरराइड करेगा।

आप NO करने के लिए अपने कस्टम दृश्य की opaque गुण सेट और संदर्भ स्पष्ट करना चाहिए इससे पहले कि आप पथ प्रस्तुत करना:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.opaque = NO; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(context, rect); 

    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    [path moveToPoint:CGPointMake(100, 33)]; 
    [path addLineToPoint:CGPointMake(200, 33)]; 
    path.lineWidth = 5; 
    [[UIColor redColor] setStroke]; 
    [path stroke]; 
} 

https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instp/UIView/opaque

एक अपारदर्शी दृश्य के साथ अपनी सीमा को भरने के लिए आशा की जाती है पूरी तरह से अपारदर्शी सामग्री-यानी, सामग्री 1.0 का अल्फा मान होना चाहिए। यदि दृश्य अपारदर्शी है और या तो अपनी सीमाएं भरता नहीं है या पूरी तरह से या आंशिक रूप से पारदर्शी सामग्री शामिल है, तो परिणाम अप्रत्याशित हैं। आप को इस संपत्ति का मान हमेशा एन पर सेट करना चाहिए यदि दृश्य पूरी तरह से या आंशिक रूप से पारदर्शी है।

+0

जब मैं इस आदेश का उपयोग करता हूं, पृष्ठभूमि कुछ कारणों से काला बनी हुई है। समाधान मेरे लिए काम नहीं करता है। यह क्यों हो सकता है? – Pete

+1

मैंने अपना जवाब सही कर दिया है। दृश्य की अपारदर्शी संपत्ति को NO पर सेट करना महत्वपूर्ण है। – iOSX

+0

बिल्कुल सही, धन्यवाद। अब अच्छा काम करता है। – Pete

1

initWithFrame में पृष्ठभूमि रंग सेट करने के लिए अपने कोड में परिवर्तित करें: विधि और drawRect के अंदर अपने कस्टम ड्राइंग करना :. यह आपकी समस्या को हल करने में सक्षम होना चाहिए। DrawRect: कस्टम ड्राइंग करने के लिए लागू विशिष्ट गुणों को सेट न करने के लिए लागू किया गया है।

@implementation MyView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor yellowColor]; 
    } 
    return self; 
} 
- (void)drawRect:(CGRect)rect{ 
    UIBezierPath *path = [[UIBezierPath alloc] init]; 
    [path moveToPoint:CGPointMake(100, 33)]; 
    [path addLineToPoint:CGPointMake(200, 33)]; 
    path.lineWidth = 5; 
    [[UIColor redColor] setStroke]; 
    [path stroke]; 

} 

@end 
1
backgroundViewController.providesPresentationContextTransitionStyle = YES; 
backgroundController.definesPresentationContext = YES; 
[overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext]; 

या

enter image description here

मैं कई घंटों के लिए संघर्ष किया। यदि यह एक मॉडल segue है, वर्तमान संदर्भ पर संपत्ति के रूप में सेट। !

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