6

我想更改 aUIViewController's内部视图的高度UINavigationController以在底部显示横幅,以免遮挡任何内容。

我认为只需更改视图中的框架,这将非常容易,viewDidLoad但这不起作用:

CGRect frame = self.view.frame;
self.view.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height - 49.0f);

我也尝试添加

[navigationController.view setAutoresizesSubviews:NO];

启动后,UINavigationController但它仍然看起来一样。

我现在能想到的唯一选择是在内部使用一个会被横幅遮挡UINavigationController的假人,但这对我来说似乎不必要地复杂。UITabBarController

有什么办法可以改变视图控制器视图的高度UINavigationController吗?

4

2 回答 2

5

无法从视图控制器中更改视图控制器的视图,但您可以使用自定义容器视图控制器:

// Create container
UIViewController* container = [[UIViewController alloc] init];

// Create your view controller
UIViewController* myVc = [[MyViewController alloc] init];

// Add it as a child view controller
[container addChildViewController:myVc];
[container.view addSubview:myVc.view];
myVc.view.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight;
myVc.view.frame = CGRectMake(0, 0, container.view.bounds.size.width, container.view.bounds.size.height-200);

// Add your banner
UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner"]];
imgView.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth| UIViewAutoresizingMaskFlexibleTopMargin;
imgView.frame = CGRectMake(0, container.view.bounds.size.height-200, container.view.bounds.size.width, 200);
[myVc.view addSubview:imgView];

现在您可以将container视图控制器添加到导航控制器而不是您的控制器。

于 2012-11-05T12:46:52.450 回答
0

Swift 5 版本的@jjv360 答案:

override func viewDidLoad() {
    super.viewDidLoad()

    let myVc = UIViewController()
    
    self.addChild(myVc)
    self.view.addSubview(myVc.view)
    myVc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    myVc.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height-200)
        
    let imgView = UIImageView(image: UIImage(named: "banner"))
    imgView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
    imgView.frame = CGRect(x: 0, y: self.view.bounds.size.height-200, width: 
    self.view.bounds.size.width, height: 200)
        myVc.view.addSubview(imgView)
}
于 2020-10-20T04:38:58.830 回答