如何使用导航控制器创建静态子视图?
问题描述:
我已经创建了一个视图控制器堆栈的导航控制器。如何使用导航控制器创建静态子视图?
我想添加一个子视图在底部保持静态(不移动),而用户之间浏览这堆视图。
就像在某些应用程序中,底部有一个“广告栏”。
我该怎么做?
答
如果我理解正确的话,这是你想要的东西:
您就可以向该通过创建包围UINavigationController的一个自定义的UIViewController。创建一个名为 “CustomViewController” 新品类,并粘贴以下代码:
接口
#import <UIKit/UIKit.h>
@interface CustomViewController : UIViewController
- (id)initWithViewController:(UIViewController*)viewController bottomView:(UIView*)bottomView;
@end
实现:
#import "CustomViewController.h"
@implementation CustomViewController
- (id)initWithViewController:(UIViewController*)viewController bottomView:(UIView*)bottomView
{
self = [super init];
if (self) {
// Set up view size for navigationController; use full bounds minus 60pt at the bottom
CGRect navigationControllerFrame = self.view.bounds;
navigationControllerFrame.size.height -= 60;
viewController.view.frame = navigationControllerFrame;
// Set up view size for bottomView
CGRect bottomViewFrame = CGRectMake(0, self.view.bounds.size.height-60, self.view.bounds.size.width, 60);
bottomView.frame = bottomViewFrame;
// Enable autoresizing for both the navigationController and the bottomView
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
bottomView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
// Add views as subviews to the current view
[self.view addSubview:viewController.view];
[self.view addSubview:bottomView];
}
return self;
}
@end
现在使用CustomViewController:
UIView *myBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
myBottomView.backgroundColor = [UIColor redColor];
CustomViewController *customViewController = [[CustomViewController alloc] initWithViewController:<yourNavigationController> bottomView:myView];
答
为什么不在与导航控制器视图相同的级别添加此静态子视图(通常是UIWindow附近的级别)。只需将它添加到最上面即可。
谢谢y欧!这正是我想要的 –
哦,,,我有问题,而实现它,我可以得到一个实施的源代码?这将如此伟大:D –
@PangHoMing如果你告诉我更多关于你如何设置你的UINavigationController的信息,我可以提供额外的代码/信息。 –