iOS:在导航栏下方添加固定图像 [英] iOS: Adding a fixed image just below the navigation bar
问题描述
感觉这应该相当简单,但到目前为止我没有尝试过任何工作。简而言之,我想在我以编程方式创建的UITableViewController中的导航栏下方添加一个固定图像。换句话说,我希望图像保持在导航栏的正下方,即使用户在桌面视图中上下滚动(它基本上是导航栏的自定义阴影)。
It feels like this should be fairly simple but nothing i've tried so far has worked. In a nutshell, I want to add a fixed image just below the navigation bar in a UITableViewController that i create programmatically. In other words, I want the image to stay just below the navigation bar even as the user scrolls up and down the table view (it's basically a custom drop-shadow for the navigation bar).
我得到的最接近的是下面的代码(在UITableViewController的init方法中),它添加了图像,但在用户滚动时不会阻止它移动。
The closest I've gotten is the code below (in the UITableViewController's init method), which adds the image but doesn't keep it from moving when the user scrolls.
// Add nav bar drop shadow
UIImage *dropShadowImage = [UIImage imageNamed:@"NavBarDropShadow.png"];
UIImageView *dropShadowView = [[UIImageView alloc] initWithImage:dropShadowImage];
[self.view addSubview:dropShadowView];
是否有一种简单的方法可以通过编程方式添加图像添加到屏幕,将其放在任何您喜欢的位置,即使用户滚动,它还能留在那里吗?感谢任何和所有输入!
Is there an easy way to add an add an image to the screen programmatically, position it wherever you like, and have it stay there even as the user scrolls? Thanks for any and all input!
推荐答案
编辑:IOS5有更好的方法来做到这一点。请查看新的 UIAppearance 协议。
IOS5 has a better way to do this. Please check out the new UIAppearance protocol.
将此代码块添加到代码中将允许您在应用程序中的所有UINavigationBars上绘制阴影。这是一个比将阴影添加为UIImageView更好的解决方案:
Adding this block of code to your code will allow you to draw your shadow on all UINavigationBars in the app. This is a better solution than adding the shadow as a UIImageView:
@implementation UINavigationBar (ShadowBar)
- (void)drawRect:(CGRect)rect {
//draw the shadow ui nav bar
UIImage *image = [UIImage imageNamed: @"UINavBarWithShadow.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
- (void)layoutSubviews {
self.frame = CGRectMake(0, 0, self.frame.size.width, 300);
}
@end
使UINavigationBar更高,从而不会削减你的内容,覆盖 layoutSubviews
并设置您需要的帧(上面的代码假设您的标题高300点)。 layoutSubviews
默认情况下不执行任何操作,但在布局视图之前调用了lazy。
To make the UINavigationBar higher and thus not clipping your content, override the layoutSubviews
and set the frame you need (the code above assumes your header is 300 points high). layoutSubviews
does nothing by default, but is "lazy" called before lay-outing the view.
有关适用于UIView(以及任何其他子类)的自定义大小/外观覆盖的详细信息,请查看这里
For more info about this custom size/look overrides that apply to UIView (and any other subclass) have a look here
这篇关于iOS:在导航栏下方添加固定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!