【问题标题】:Adding shadow to a UINavigationControllerer向 UINavigationControllerer 添加阴影
【发布时间】:2011-04-06 02:22:07
【问题描述】:

我正在尝试向 UINavigationController 添加阴影。我拥有的是基于 NavController 的,我正在尝试像在游戏中心一样添加阴影。到目前为止我有这个。它在 UINavigationnBar 上工作,但我试图让它在整个应用程序中工作。

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,你就会明白我在说什么。

【问题讨论】:

  • 这是在 UINavigationController 子类中吗?
  • 我尝试了一个子类,但它也不起作用。不,它在 viewDidLoadAnimated 的 UIViewController 中:

标签: iphone objective-c uinavigationcontroller shadow


【解决方案1】:

我相信你应该把它添加到

   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

祝你好运

【讨论】:

  • 我尝试了UINavigationController.view.layer,但它不起作用,但我认为不会。将它放在绘图矩形中不起作用。我没有什么可以设置子层的。我试过[self addSublayer:newShadow] 我对如何应用它感到困惑。其他一切都有意义
  • 如果您使用的是绘制矩形,您应该尝试将阴影添加到图层中。但是要使用 Quartz 2d 绘制一个矩形并在其阴影处。我将编辑我的答案并尝试为您提供帮助。
  • 谢谢,我从来没有玩过 Quartz,所以这是我第一次玩。非常感谢
  • 添加了链接。第一个很简单。玩它,你会发现它不是那么大的莳萝
  • 你说的我一直在用。我更新了我的问题。见上文^
【解决方案2】:

您可以继承 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 视图中,您可以有效地将它覆盖在视图控制器正在显示的任何内容之上。您可以通过检查主屏幕的比例来进一步改进这一点,以适当地计算阴影视图的高度,以便它在所有设备上都能正确显示,无论是否有视网膜显示器。

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多