UIBarButtons太靠近UIPopoverController内UINavigationBar的边缘

时间:2013-05-24 19:28:01

标签: ios uinavigationbar uipopovercontroller

UIPopoverController内UINavigationBar中的UIBarButtonItem包含导航栏的左侧和右侧:

Nav Bar

这是我们实现的CustomPopoverController。它曾用于拥抱导航栏的顶部,但configurePopoverNavBar修复了它。

@implementation CMCommentPopoverController

- (id)initWithCaseId:(NSString *)cid andViewController:(CMNoteViewController *)cv forView:(UIView *)v {


    self = [super initWithContentViewController:[[UINavigationController alloc] init]];

    self.popoverNav= (UINavigationController *)self.contentViewController;

    self.caseId = cid;
    self.dvController = cv;

    //size the popover
    CGRect popoverRect = [CMMiscUtil getPopoverRect];

    [self setPopoverContentSize:CGSizeMake(popoverRect.size.width, popoverRect.size.height)];
    [self presentPopoverFromRect:CGRectMake(popoverRect.origin.x, popoverRect.origin.y, popoverRect.size.width, popoverRect.size.height) inView:v permittedArrowDirections:0 animated:YES];

    [self setUpNav];

    return self;
}

- (void) setUpNav {

    [self.popoverNav pushViewController:self.dvController animated:NO];
    self.popoverNav.navigationBar.topItem.title = @"Comments";

    //add the buttons to the nav bar of the popover nav controller
    self.popoverNav.navigationBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add Comment" style:UIBarButtonItemStylePlain target:self action:@selector(notesAction:)];
    self.popoverNav.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(closeAction:)];

    [self configurePopoverNavBar:self.popoverNav];
}


-(void) configurePopoverNavBar:(UINavigationController *)popoverNav {

    UINavigationBar *navigationBar = popoverNav.navigationBar;
    UIView *contentView = nil;

    for (UIView *view in popoverNav.view.subviews) {
        if ([[NSString stringWithFormat:@"%@", [view class]] isEqualToString:@"UILayoutContainerView"])
            contentView = 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+50 + navigationBar.frame.size.height)];

    [popoverNav.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];

}

...

2 个答案:

答案 0 :(得分:0)

使用UIButton创建自定义视图,并使用此自定义视图创建UIBarButtonItem。

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 80)];

UIButton *customButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 70)]; 
[customButton setTitle : @"Add Comment"];
[customButton addTarget:self action:@selector(notesAction:) forControlEvents:UIControlEventTouchUpInside]; 
[customView addSubview:customButton];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:customView]; 

self.popoverNav.navigationBar.topItem.rightBarButtonItem = rightBarButton;

根据您的要求更改customView和customButton框架。

答案 1 :(得分:0)

通过将UINavigationBar置于扩展10px的UIView内来解决此问题:

CGRect navBarFrame = navigationBar.frame;
navBarFrame.origin.y = 0;

UIView * navBarContainer = [[UIView alloc] initWithFrame:navBarFrame];
navBarContainer.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"header.png"]];

[navigationBar setFrame:CGRectMake(navBarFrame.origin.x + 10, navBarFrame.origin.y, navBarFrame.size.width - 20, navBarFrame.size.height)];

...

[navBarContainer addSubview:navigationBar];

[contentView addSubview:navBarContainer];

...