我已经创建了一个带有视图控制器堆栈的导航控制器。
我想在底部添加一个子视图,该子视图在用户在这个视图堆栈之间导航时保持静态(不移动)。
就像一些应用程序中,在底部有一个“广告栏”。
我该如何实现这个功能?
我想在底部添加一个子视图,该子视图在用户在这个视图堆栈之间导航时保持静态(不移动)。
就像一些应用程序中,在底部有一个“广告栏”。
我该如何实现这个功能?
如果我理解正确,这就是你想要的:
你可以通过创建一个自定义的UIViewController来实现这个效果,该UIViewController包含UINavigationController。新建一个名为“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];