简体   繁体   English

iOS:在导航栏正下方添加固定图像

[英]iOS: Adding a fixed image just below the navigation bar

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.简而言之,我想在我以编程方式创建的 UITableViewController 中的导航栏下方添加一个固定图像。 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).换句话说,我希望图像保持在导航栏下方,即使用户上下滚动表格视图(它基本上是导航栏的自定义阴影)。

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.我得到的最接近的是下面的代码(在 UITableViewController 的 init 方法中),它添加了图像,但在用户滚动时不会阻止它移动。

// 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?有没有一种简单的方法可以以编程方式将图像添加到屏幕上,position 可以放在任何你喜欢的地方,并且即使用户滚动它也能保持在那里? Thanks for any and all input!感谢您的任何和所有输入!

You can make a subclass or a category on UINavigationBar, and have it add the image in the init or drawRect methods.您可以在 UINavigationBar 上创建一个子类或类别,并让它在initdrawRect方法中添加图像。 If you think about it, you're trying to add a shadow to the navigation bar, not to the UITableView, so it makes sense to modify the navbar, not the table.如果您考虑一下,您正在尝试向导航栏添加阴影,而不是 UITableView,因此修改导航栏而不是表格是有意义的。

EDIT: IOS5 has a better way to do this.编辑: IOS5 有更好的方法来做到这一点。 Please check out the new UIAppearance protocol.请查看新的UIAppearance协议。

Adding this block of code to your code will allow you to draw your shadow on all UINavigationBars in the app.将此代码块添加到您的代码将允许您在应用程序中的所有 UINavigationBars 上绘制阴影。 This is a better solution than adding the shadow as a UIImageView:这是比将阴影添加为 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

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).要使 UINavigationBar 更高,从而不剪裁您的内容,请覆盖layoutSubviews并设置您需要的框架(上面的代码假设您的 header 高 300 点)。 layoutSubviews does nothing by default, but is "lazy" called before lay-outing the view. layoutSubviews默认情况下什么都不做,但在布局视图之前被“惰性”调用。

For more info about this custom size/look overrides that apply to UIView (and any other subclass) have a look here有关适用于 UIView(和任何其他子类)的自定义尺寸/外观覆盖的更多信息,请查看此处

You are adding your dropShadowView to self.view that in your case is the view of an UITableViewController.您正在将您的 dropShadowView 添加到 self.view,在您的情况下是 UITableViewController 的视图。 It means that your self.view is an UITableView so when you scroll up and down you scroll the dropShadowView as well because is inside the tableView.这意味着您的 self.view 是 UITableView 因此,当您上下滚动时,您也会滚动 dropShadowView,因为它位于 tableView 内。

Try to write a custom UIViewController and add two subviews: one is the dropShadowView and the other one is your table.尝试编写自定义 UIViewController 并添加两个子视图:一个是 dropShadowView,另一个是您的表格。

Have a look at this similar question I answered a while back.看看我不久前回答的这个类似的问题。 It should do exactly what you want with little customization.它应该完全符合您的要求,几乎不需要定制。

Transparent View at the Top of a UITableView UITableView 顶部的透明视图

Dont make a UITableView the main view ie.不要以UITableView为主视图,即。 the view outlet, set that to a UIView that contains a UITableView , then make your controller a subclass of UIViewController , and make it conform to UITableViewDataSource and UITableViewDelegate . the view outlet, set that to a UIView that contains a UITableView , then make your controller a subclass of UIViewController , and make it conform to UITableViewDataSource and UITableViewDelegate . in your nib, set up your view so that you have a UIImageView at the top and a UITableView below.在您的笔尖中,设置您的视图,以便您在顶部有一个 UIImageView,在下面有一个 UITableView。 and set the delegate and datasource to your file's owner .并将delegatedatasource设置为file's owner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM