iOS: Adding a fixed image just below the navigation bar

牧云@^-^@ 提交于 2019-12-03 20:36:10

EDIT: IOS5 has a better way to do this. Please check out the new UIAppearance protocol.

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

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.

For more info about this custom size/look overrides that apply to UIView (and any other subclass) have a look here

You can make a subclass or a category on UINavigationBar, and have it add the image in the init or drawRect methods. 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.

You are adding your dropShadowView to self.view that in your case is the view of an 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.

Try to write a custom UIViewController and add two subviews: one is the dropShadowView and the other one is your table.

Ben

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

Dont make a UITableView the main view ie. 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. and set the delegate and datasource to your file's owner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!