我已經創建了一個視圖控制器堆棧的導航控制器。如何使用導航控制器創建靜態子視圖?
我想添加一個子視圖在底部保持靜態(不移動),而用戶之間瀏覽這堆視圖。
就像在某些應用程序中,底部有一個「廣告欄」。
我該怎麼做?
我已經創建了一個視圖控制器堆棧的導航控制器。如何使用導航控制器創建靜態子視圖?
我想添加一個子視圖在底部保持靜態(不移動),而用戶之間瀏覽這堆視圖。
就像在某些應用程序中,底部有一個「廣告欄」。
我該怎麼做?
如果我理解正確的話,這是你想要的東西:
您就可以向該通過創建包圍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的信息,我可以提供額外的代碼/信息。 –