简体   繁体   English

带有UITabBarController的ABPeoplePickerNavigationController在iOS8中无法正确显示

[英]ABPeoplePickerNavigationController with UITabBarController is not showing correctly in iOS8

I have an application in which UITabBarController as a rootViewController with two controllers. 我有一个应用程序,其中UITabBarController作为rootViewController与两个控制器。 One is an empty controller and the second one is Picker controller extends from ABPeoplePickerNavigationController. 一个是空控制器,第二个是Picker控制器,从ABPeoplePickerNavigationController扩展。 The problem is view is going behind the tab bar and due to that view is cutting off from the bottom. 问题是视图正在标签栏后面,并且由于该视图从底部切断。 I just highlighted the area in the screenshot: 我刚刚在屏幕截图中突出显示了该区域:

在此输入图像描述

Any help would be highly appreciated. 任何帮助将受到高度赞赏。

Officially ABPeoplePickerNavigationController doesn't support subclassing: link here 正式ABPeoplePickerNavigationController不支持子类化: 链接在这里

Subclassing Notes
The ABPeoplePickerNavigationController class does not support subclassing.

However, the problem is that the view of your ABPeoplePickerNavigationController subclass is extending under the tab bar. 但是,问题是您的ABPeoplePickerNavigationController子类的视图正在标签栏下展开。 You can, for example, change it's size at runtime in this way 例如,您可以通过这种方式在运行时更改它的大小

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGRect rect = self.view.bounds;
    rect.size.height = rect.size.height - 40;
    self.view.frame = rect;
}

Note, the 40 is only an example, you should calculate the height of your tab bar controller because it can change for other screen dimensions and rotations. 请注意,40只是一个示例,您应该计算标签栏控制器的高度,因为它可以更改其他屏幕尺寸和旋转。

Or, better, you can search for the underlying UITableView instance and set the contentInset property. 或者,更好的是,您可以搜索基础UITableView实例并设置contentInset属性。

EDIT This seems to have problems with the status bar, since the ABPeoplePickerNavigationController is unable to extend the navigation bar under the status bar if you change it's frame 编辑这似乎与状态栏有问题,因为如果更改它的框架, ABPeoplePickerNavigationController无法扩展状态栏下的导航栏

In both this cases, however, your app will probably be rejected, because you are subclassing a class that explicitly forbids it. 但是,在这两种情况下,您的应用程序可能会被拒绝,因为您正在继承一个明确禁止它的类。

A better and "legal" way to add it is to use a container view controller 添加它的更好和“合法”的方法是使用容器视图控制器

Look at This Example 看看这个例子

Create a new view controller, add a container view, then add the ABPeoplePickerNavigationController in this way: 创建一个新的视图控制器,添加一个容器视图,然后以这种方式添加ABPeoplePickerNavigationController

-(void)viewDidLoad
{
    [super viewDidLoad];

    // create the ABPeoplePickerNavigationController instance
    ABPeoplePickerNavigationController *controller = [[ABPeoplePickerNavigationController alloc] init];

    // add a new child view controller to self
    [self addChildViewController:controller];

    // alert the child that it has been added to the father
    [controller didMoveToParentViewController:self];

    // update the child view frame to fit into the containerView
    controller.view.frame = self.containerView.bounds;

    // translate autoresizing mask into constraints, this is not needed but I usually do because is more pratical
    [controller.view setTranslatesAutoresizingMaskIntoConstraints:YES];

    // add the `ABPeoplePickerNavigationController` view to the container
    [self.containerView addSubview:controller.view];

}

Even in this way ABPeoplePickerNavigationController has problems with the status bar (it doesn't extends correctly under it), so I constrained the container view to the Top Layout Guide and changed the color of the main view of ContainerViewController to fit with the color of the navigation bar 即使这样, ABPeoplePickerNavigationController也会出现状态栏问题(它在它下面没有正确扩展),所以我将容器视图限制在Top Layout Guide中,并改变了ContainerViewController主视图的颜色以适应颜色。导航栏

In viewWillAppear viewWillAppear中

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.topViewController.extendedLayoutIncludesOpaqueBars = YES;
}

In viewDidAppear viewDidAppear中

- (void)viewDidAppear:(BOOL)animated
{
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
    CGRect tabBarFrame = self.tabBarController.tabBar.frame;        
    self.view.frame = CGRectMake(0, statusBarFrame.size.height, self.view.frame.size.width, CGRectGetMinY(tabBarFrame) - statusBarFrame.size.height);
}

A little late to the party, but here's my answer to the problem or at least some additional input for you to work out a complete solution. 派对有点晚了,但这是我对问题的回答,或者至少是一些额外的输入,让你找到一个完整的解决方案。

The problem to me seems to be that because UIPeoplePickerNavigationController is not subclass-able or at least is not recommended to be by Apple, you can't use it entirely anyway you like. 我的问题似乎是因为UIPeoplePickerNavigationController不是子类,或者至少不建议由Apple使用,所以你无论如何都不能完全使用它。 What I mean is UIPeoplePickerNavigationController is meant to be used as a modal view, which should be presented full screen on iOS and on top of every other view controller. 我的意思是UIPeoplePickerNavigationController意味着用作模态视图,应该在iOS上和所有其他视图控制器上全屏显示。 You shouldn't try to use it as a push view controller on a navigation controller stack. 您不应该尝试将其用作导航控制器堆栈上的推式视图控制器。

So my suggestion is quite straight-forward. 所以我的建议很简单。 You should simply use your UITabBarController as the receiver of the presentViewController:animated:completion: method. 您应该只使用您的UITabBarController作为presentViewController:animated:completion:方法的接收者。 That will take care of presenting the modal on top of the tab bar. 这将负责在标签栏顶部显示模态。

You can access the UITabBarController through the tabBarController property of your current view controller: 您可以通过当前视图控制器的tabBarController属性访问UITabBarController

ABPeoplePickerNavigationController *peoplePickerController = [ABPeoplePickerNavigationController new];
[peoplePickerController setPeoplePickerDelegate:self];
[self.tabBarController presentViewController:peoplePickerController animated:YES completion:^{}]

Note: I'm writing this from memory so there may be some method naming mistakes. 注意:我是从内存中写的,所以可能有一些方法命名错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM