2

我们的 UIPopoverController 内的 UINavigationBar 中的UIBarButtonItems 拥抱导航栏的左右两侧:

导航栏

这是我们实现的 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];

}

...
4

2 回答 2

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];

...
于 2013-05-24T20:55:32.363 回答
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 框架。

于 2013-05-24T20:19:43.673 回答