gpt4 book ai didi

ios - 如何使用导航 Controller 创建静态 subview ?

转载 作者:可可西里 更新时间:2023-11-01 06:19:50 25 4
gpt4 key购买 nike

我创建了一个带有一堆 View Controller 的导航 Controller 。

我想在底部添加一个 subview ,当用户在这组 View 之间导航时它保持静态(不移动)。

像某些应用程序一样,底部有一个“广告栏”。

我该怎么做?

最佳答案

如果我没理解错的话,这就是你想要的:

UIView below UINavigationController or below TabBar

您可以通过创建一个包含 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];

关于ios - 如何使用导航 Controller 创建静态 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10434137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com