iOS:在导航栏下方添加固定图像
感觉这应该相当简单,但到目前为止我尝试过的都没有成功。简而言之,我想在我以编程方式创建的 UITableViewController 中的导航栏下方添加一个固定图像。换句话说,我希望图像保持在导航栏的正下方,即使用户在表格视图中上下滚动(它基本上是导航栏的自定义阴影)。
我得到的最接近的是下面的代码(在 UITableViewController 的 init 方法中),它添加了图像,但在用户滚动时并没有阻止它移动。
// Add nav bar drop shadow
UIImage *dropShadowImage = [UIImage imageNamed:@"NavBarDropShadow.png"];
UIImageView *dropShadowView = [[UIImageView alloc] initWithImage:dropShadowImage];
[self.view addSubview:dropShadowView];
有没有一种简单的方法可以以编程方式将图像添加到屏幕上,将其放置在您喜欢的任何位置,并且即使用户滚动也可以将其保留在那里?感谢您的任何意见!
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).
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在 UINavigationBar 上创建一个子类或类别,并让它在
init
或drawRect
方法中添加图像。如果您考虑一下,您会尝试向导航栏添加阴影,而不是向 UITableView 添加阴影,因此修改导航栏而不是表格是有意义的。You can make a subclass or a category on UINavigationBar, and have it add the image in the
init
ordrawRect
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.您将 dropShadowView 添加到 self.view 中,在您的情况下是 UITableViewController 的视图。这意味着您的 self.view 是一个 UITableView,因此当您上下滚动时,您也会滚动 dropShadowView,因为它位于 tableView 内部。
尝试编写一个自定义 UIViewController 并添加两个子视图:一个是 dropShadowView,另一个是您的表格。
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.
看看我不久前回答的这个类似问题。它应该完全满足您的需求,几乎不需要定制。
UITableView 顶部的透明视图
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
作为主视图,即。view
出口,将其设置为包含UITableView
的UIView
,然后使您的控制器成为UIViewController
的子类,并使其符合UITableViewDataSource
和UITableViewDelegate
。在你的笔尖中,设置你的视图,以便你在顶部有一个 UIImageView ,在下面有一个 UITableView 。并将委托
和数据源
设置为您的文件所有者
。Dont make a
UITableView
the main view ie. theview
outlet, set that to aUIView
that contains aUITableView
, then make your controller a subclass ofUIViewController
, and make it conform toUITableViewDataSource
andUITableViewDelegate
. in your nib, set up your view so that you have a UIImageView at the top and a UITableView below. and set thedelegate
anddatasource
to yourfile's owner
.编辑:IOS5 有更好的方法来做到这一点。请查看新的 UIAppearance协议。
将此代码块添加到您的代码中将允许您在应用程序中的所有 UINavigationBar 上绘制阴影。这是比将阴影添加为 UIImageView 更好的解决方案:
要使 UINavigationBar 更高,从而不剪切您的内容,请覆盖
layoutSubviews
并设置您需要的框架(上面的代码假设您的标题为 300点高)。layoutSubviews
默认情况下不执行任何操作,而是在布局视图之前“惰性”调用。有关适用于 UIView (和任何其他子类)的自定义大小/外观覆盖的更多信息,请查看 此处
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:
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