Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 200
LightNight Avatar asked Mar 14 '12 11:03

LightNight


2 Answers

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.

like image 125
Ash Furrow Avatar answered Jan 27 '23 14:01

Ash Furrow


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

like image 37
Hiren Avatar answered Jan 27 '23 15:01

Hiren