2012-02-27 54 views
0

我有一個UIView W/C我返回作爲視圖在我viewForHeaderInSection。現在,我想在視圖的右下方製作一個小的透明正方形,以便可以看到單元格。我怎樣才能實現這樣的事情?我需要使用這個CGContextSetBlendMode嗎?任何人都可以闡明這一點。使UIView的一部分變得透明

+0

你可以把它全部是透明的,並添加一個UIImageView作爲與具有一個PNG背景部分透明。 – 2012-02-27 09:28:39

+0

你可以從結果中添加藍圖嗎?很難形象化。 – mariusLAN 2012-02-27 09:29:08

回答

0

該視圖作爲子視圖插入到SwitchViewController.view中。所以,它們將始終出現在SwitchViewControllers視圖的上方。另一個視圖被刪除,一次只會出現一個視圖

例如,這裏

[yellowViewController.view removeFromSuperview]; 
[self.view insertSubview:blueViewController.view atIndex:0]; 

觀點似乎在默認情況下它的子視圖的背後,

insertSubview:atIndex: 

不能把它認爲自己的背後,作爲一個視圖不會在自己的子視圖列表中包含。

+0

讓我知道如果這個代碼工作..我有同樣的概率,我已經通過堆棧解決 – 2012-02-27 09:33:54

0

這是一個UIView子類的解決方案。爲了這個工作,你必須保留superviews backgroundColor屬性爲nil女巫是默認值。如果此UIView子類在故事板中,請確保在屬性檢查器中將背景色設置爲「清除顏色」。

- (void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    UIColor *blackBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; 
    CGContextSetFillColorWithColor(ctx, blackBackgroundColor.CGColor); 
    CGContextFillRect(ctx, rect); 
    CGRect transparentPart = CGRectMake(10, 10, 120, 80); 
    CGContextClearRect(ctx, transparentPart); 
} 
0
CALayer *layer = [[CALayer alloc] init]; 
[layer setFrame:yourBoundsInView]; 

[[yourView layer] setMask:layer]; 

或重寫drawRect方法,並在末尾添加的代碼以下行

UIBezierPath *seeThrough = [UIBezierPath bezierPathWithRect:seeThroughRect]; 
[[UIColor colorWithWhite:1 alpha:0] set]; 
[seeThrough fillWithBlendMode:kCGBlendModeSourceIn alpha:1];