Iphone 更改UINavigationController内部的视图高度
我想更改Iphone 更改UINavigationController内部的视图高度,iphone,ios,uiviewcontroller,uinavigationcontroller,Iphone,Ios,Uiviewcontroller,Uinavigationcontroller,我想更改UINavigationController内部的UIViewController视图的高度,以便在底部显示横幅,这样它就不会遮挡任何东西 我以为只要在viewdiload中更改视图的框架就可以很容易做到这一点,但这并不奏效: CGRect frame = self.view.frame; self.view.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height - 4
UINavigationController
内部的UIViewController
视图的高度,以便在底部显示横幅,这样它就不会遮挡任何东西
我以为只要在viewdiload
中更改视图的框架就可以很容易做到这一点,但这并不奏效:
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
后,它看起来仍然一样
我现在能想到的唯一选择是在虚拟uitabarcontroller
中使用UINavigationController
,该虚拟控制器将被横幅遮住,但对我来说似乎不必要地复杂
是否有任何方法可以更改视图控制器在视图控制器内的视图高度?无法从视图控制器内更改视图控制器的视图,但可以使用自定义容器视图控制器:
// 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];
现在,您可以将
容器
视图控制器添加到导航控制器,而不是您的导航控制器。Swift 5版本的@jv360答案:
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)
}
您正在将UIViewController添加到UINavigationController吗?我想您可以从这个链接中了解一些情况。。查看视图上的布局约束。默认情况下,它们将填充导航控制器空间。无论导航控制器上的AutoResizeSubView如何,它都是更可自定义的tho(您可以将自己的视图作为横幅),如果您没有访问视图控制器的权限(如使用表视图),它也很好。。。