UIPopoverController和UINavigationController削减了角落

Lig*_*ght 9 iphone objective-c uinavigationcontroller uipopovercontroller

我的弹出窗口显示有问题.之后initWithContentViewController:,presentPopoverFromBarButtonItem:permittedArrowDirections:animated:它切断导航栏的角落.我应该怎么解决?谢谢.

剪切导航栏的角落

这是我正在使用的代码

    NavContr *nav = [NavContr new];
    nav.navigationBar.backgroundColor = [UIColor redColor];
    UIPopoverController *tempPop = [[UIPopoverController alloc] initWithContentViewController:nav];
    [tempPop presentPopoverFromBarButtonItem:mainButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
Run Code Online (Sandbox Code Playgroud)

编辑:我已经解决了这个问题:

+ (void)configure:(UINavigationController *)navController {
    UINavigationBar *navigationBar = navController.navigationBar;       
    UIView *contentView = nil;

    for (UIView *view in navController.view.subviews) {
        if ([[NSString stringWithFormat:@"%@", [view class]] isEqualToString:@"UILayoutContainerView"])
            contentView = view;
    }

    // setting frame to navigation bar and content view
    [navigationBar setFrame:CGRectMake(navigationBar.frame.origin.x, 0, navigationBar.frame.size.width, navigationBar.frame.size.height)];
    [contentView setFrame:CGRectMake(contentView.frame.origin.x, 0, contentView.frame.size.width, contentView.frame.size.height + navigationBar.frame.size.height)];

    [navController.view bringSubviewToFront:contentView];

    for (UIView *customView in contentView.subviews)
        customView.frame = CGRectMake(customView.frame.origin.x, customView.frame.origin.y + navigationBar.frame.size.height, customView.frame.size.width, customView.frame.size.height);

    [contentView addSubview:navigationBar];
    [contentView bringSubviewToFront:navigationBar];
}
Run Code Online (Sandbox Code Playgroud)

Ash*_*row 10

这可能是因为你没有根视图控制器,或者以其它方式摆弄导航控制器.这就是你应该如何设置popover:

MyCustomViewController *viewController = [[UIViewController alloc] initWithNibName:@"MyCustomViewController" bundle:nil]; //or storyboard or whatever
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController]; //you should have a root view controller before displaying the popover
tintColor = [UIColor redColor];
UIPopoverController *tempPop = [[UIPopoverController alloc] initWithContentViewController:nav];
[tempPop presentPopoverFromBarButtonItem:mainButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
Run Code Online (Sandbox Code Playgroud)

这里有一些非常重要的事情:

  • 导航控制器在显示之前应具有根视图控制器.
  • 此代码使用标准UINavigationController实例.根据文档,你不应该是子类UINavigationController,也不应该尝试重新发明轮子.Apple创建了一个复杂而全面的框架UIKit,您可以使用它构建出色的应用程序.如果你试着走出盒子,那么你将为自己创造大量的工作而没有任何明显的好处.
  • 这是使用类的tintColor属性UINavigationBar.如果色调不足以用于UI,您还可以手动设置背景图像(请参阅文档).

如果要使用导航控制器创建弹出窗口,请使用内置UINavigationController类.不要对它进行子类化,也不要重新发明它.要自定义外观navigationBar,请使用类中的UI_APPEARANCE_SELECTOR方法UINavigationBar.