2011-04-06 211 views
0

我試圖將陰影添加到UINavigationController。我已經基於NavController,我試圖添加像遊戲中心一樣的陰影。到目前爲止,我有這個。它工作在UINavigationnBar但我試圖讓它在整個應用程序中工作。將陰影添加到UINavigationControllerer

CGColorRef darkColor = [[UIColor blackColor] colorWithAlphaComponent:.5f].CGColor; 
CGColorRef lightColor = [UIColor clearColor].CGColor; 

CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease]; 
newShadow.frame = CGRectMake(0, 44, 320, 10); 
newShadow.colors = [NSArray arrayWithObjects:(id)darkColor, (id)lightColor, nil]; 

CALayer *superlayer = self.newShadow.superlayer; 
[self.newShadow removeFromSuperlayer]; 
[superlayer addSublayer:self.newShadow]; 
[self.navigationBar.layer addSublayer:superlayer]; 

它直接在UINavigationBar上工作,但應用於NavigationController項目失敗。它建立,但不會增加陰影。有任何想法嗎?

編輯

我一直在嘗試不同的方法來此。我通過使用形狀成功創建了漸變。

@implementation UINavigationBar (UINavigationBarCategory) 
- (void)drawRect:(CGRect)rect { 
UIImage *backgroundImage; 
backgroundImage = [UIImage imageNamed:@"nav.png"]; 
[backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 


CGContextRef context = UIGraphicsGetCurrentContext(); 
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB(); 
size_t num_locations = 2; 
CGFloat locations[2] = { 1.0, 0.0 }; 
CGFloat components[8] = { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.5 }; 

CGGradientRef myGradient = CGGradientCreateWithColorComponents(myColorspace, components, locations, num_locations); 

CGPoint myStartPoint, myEndPoint; 
myStartPoint.x = 0.0; 
myStartPoint.y = 0.0; 
myEndPoint.x = 0.0; 
myEndPoint.y = 54.0; 
CGContextDrawLinearGradient (context, myGradient, myStartPoint, myEndPoint, 1); 
} 

我不能讓下面UINavigationBar的梯度和它覆蓋的圖像。我似乎無法得到這個工作。我試圖做的是添加相同的影子游戲中心。我嘗試了幾種不同的方法。我所需要做的就是讓它位於UINavigationBar下面,讓圖像位於頂部,並將一小部分陰影置於UITableView的頂部,以便在UITableView上方向上滾動。如果你啓動Game Center,你會看到我正在談論的內容。

+1

這是在'UINavigationController'子類嗎? – BoltClock 2011-04-06 14:06:26

+0

我試過一個子類,但它也沒有工作。不,它在viewDidLoadAnimated中的UIViewController: – Frankrockz 2011-04-06 14:22:29

回答

0

我相信你應該把它添加到

UINavigationController.view.layer 

由於UINavigationController的他是不是一個UIView孩子。

如果你已經這樣做了,一個其他的方式來實現導航欄,始終遍佈應用程序是使用UINavigationBarCategory:

您在前人的精力將委託結束這個步伐 - 的@end

@implementation UINavigationBar (UINavigationBarCategory) 
- (void)drawRect:(CGRect)rect { 
    //implement your design 
} 
@end 

編輯

在這個環節上,你可以找到關於如何繪製條基本信息: An iPhone Graphics Drawing Tutorial using Quartz 2D

,在這裏你可以找到如何添加陰影:adding shadow with quartz 2d

好運

+0

我試過'UINavigationController.view.layer',但它沒有工作,但我認爲不會。將它放在繪製矩形中不起作用。我沒有設置子圖層。我試過'[self addSublayer:newShadow]'我對如何應用它感到困惑。如果你正在使用繪製矩形,你應該嘗試添加陰影而不是圖層,否則其他所有東西都會生成 – Frankrockz 2011-04-06 16:00:36

+0

。但用Quartz 2d繪製一個矩形和一個陰影。我會編輯我的答案,並試圖幫助你。 – shannoga 2011-04-06 16:12:26

+0

謝謝,我從來沒有玩過Quartz,所以這是我第一次。非常感謝 – Frankrockz 2011-04-06 16:13:49

0

你可以繼承的UIView,並使用現有的代碼通過覆蓋-drawRect:繪製陰影。然後在需要的地方創建它並將其添加到導航控制器的視圖中。

考慮這個例子

CGFloat screenWidth = [UIScreen mainScreen].applicationFrame.size.width; 
ShadowView *shadowView = [[ShadowView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, screenWidth, 20.0f)]; 
[self.navigationController.view insertSubview:shadowView atIndex:0]; 

通過索引0將其添加到navigationController的視圖,你有效地覆蓋它無論視圖控制器顯示的頂部。您可以通過檢查主屏幕的比例來適當地計算陰影視圖的高度,以便它能夠在所有設備上正確顯示,而無需使用視網膜顯示,從而進一步改善這一點。