我正在使用 tabBarController 和 navigationController 我在 TabBarController 的其中一个 viewController 上显示一个 Half ModalView 作为 subview 。 注意我使用的是 iPhone-5 模拟器。我的问题是 topViewController 的高度是 504,尽管它应该是 568 我无法确定将它显示在 tabBar 上方的正确位置。
UIViewController *viewCon = [(UINavigationController*)[tabBarController selectedViewController] topViewController];
UIViewController*vc=[tabBarController.viewControllers objectAtIndex:0];
NSLog(@"vc,%f",vc.view.frame.size.height);// prints 568
if (viewCon.childViewControllers.count == 0) {
UIStoryboard* story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
_modal = [story instantiateViewControllerWithIdentifier:@"HalfModal"];
[viewCon addChildViewController:_modal];
_modal.view.frame = CGRectMake(0, screenHeight ,screenWidth, screenHeight);
NSLog(@"TopviewController height ,%f",CGRectGetHeight(viewCon.view.frame));//prints 504
frame = CGRectMake(0, 0, screenWidth, screenHeight);
_view2 = [[UIView alloc]initWithFrame:frame];
[viewCon.view addSubview:_view2];
[viewCon.view addSubview:_modal.view];
[UIView animateWithDuration:0.5
animations:^{
[_view2 setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
_modal.view.frame = CGRectMake(0, screenHeight/2, 320, 284);
}
completion:^(BOOL finished) {
[_modal didMoveToParentViewController:viewCon];
}];
最佳答案
首先我会建议避免这样的事情
CGRectMake(0, screenHeight/2, 320, 284);
(用于设置元素大小/位置的常量),您必须从 UIScreen 对象计算它。
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width
其次:如果你想显示最顶层的 View ,你可以尝试将它添加到 UIWindow
关于ios - 如何在 tabBarController 上方显示半个 modalView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29228629/