LightNight
LightNight

Reputation: 1625

UIPopoverController and UINavigationController cuts corners

I have a problem with the display of my popover. After initWithContentViewController: and presentPopoverFromBarButtonItem:permittedArrowDirections:animated: it cuts corners of the navigation bar. How should I fix it?? Thanks.

Clipping corners of navigation bar

This is the code I'm using

    NavContr *nav = [NavContr new];
    nav.navigationBar.backgroundColor = [UIColor redColor];
    UIPopoverController *tempPop = [[UIPopoverController alloc] initWithContentViewController:nav];
    [tempPop presentPopoverFromBarButtonItem:mainButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];

EDIT: I have resolved this problem:

+ (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];
}

Upvotes: 9

Views: 4140

Answers (4)

Hiren
Hiren

Reputation: 12780

I get the solution before add CALayer the UIPopOverController shows like rounded
after adding below lines in table view class i get the following UIPopOverController

#import <QuartzCore/QuartzCore.h>

CALayer *imageLayer2 = self.tableView.layer;
[imageLayer2 setCornerRadius:-20];
[imageLayer2 setBorderWidth:1];

non rounded

Try it in your project may be it works!!
Thanx

Upvotes: 3

Jonathan Barlow
Jonathan Barlow

Reputation: 1075

If you are specifying the Rect where the popover appears, we've found that using decimals can result in weird distortions like that. Be sure you're using whole number for origin and size.

Upvotes: 0

Ash Furrow
Ash Furrow

Reputation: 12421

This is probably because you have no root view controller, or are otherwise fiddling with the navigation controller in ways it was not meant to be played with. This is how you ought to be setting up the 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];

There are a few very important things going on here:

  • Your navigation controller should have a root view controller before you display it.
  • This code is using a standard UINavigationController instance. According to the documentation, you should not subclass UINavigationController, nor should you try and reinvent the wheel. Apple has created a complex and comprehensive framework, UIKit, that you can use to build amazing apps. If you try and step outside the box, you'll be creating an awful lot of work for yourself without any appreciable benefit.
  • This is using the tintColor property of the UINavigationBar class. If the tint is insufficient for your UI, you can also set the background image manually (refer to the docs).

If you want to make a popover with a navigation controller, use the built-in UINavigationController class. Don't subclass it and don't reinvent it. To customize the appearance of the navigationBar, use the UI_APPEARANCE_SELECTOR methods in the UINavigationBar class.

Upvotes: 10

Kuldeep
Kuldeep

Reputation: 2589

enter image description here

I have tried & replicate the issue you are facing, made some R&D. It's due to the line of code below :

nav.navigationBar.backgroundColor = [UIColor redColor];

While you set the background color of the navigation bar it will behave weird due the native shape of the pop up. Try and remove the below line, you will definitely have issue resolved.

Upvotes: 0

Related Questions