如何在UISplitViewController的工具栏右侧添加按钮?

时间:2011-06-01 21:43:38

标签: iphone uisplitviewcontroller uibarbuttonitem uitoolbar

尝试在基于拆分视图的应用中向detailviewcontroller的工具栏右侧添加一个按钮。我使用灵活的空间将它放到右侧。在肖像中,它工作正常,但在横向(当菜单按钮消失时),按钮被移动,使其一半离开屏幕。

以下是相关代码(DetailViewController.m):

- (void) viewDidLoad 
{
    // initialize toolbar
    toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake( 0, 0, 768, 44 )];
    titleLabel = [[UILabel alloc] initWithFrame: CGRectMake( 284, 3, 200, 35 )];
    titleLabel.text = @"Title & Location";
    titleLabel.textAlignment = UITextAlignmentCenter;
    [toolbar addSubview: titleLabel];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle: @"Add Event" style: UIBarButtonItemStyleDone target: rootController action: @selector(parseDone)];
    NSArray *buttonArray = [NSArray arrayWithObjects: flexibleSpace, doneButton, nil];
    [toolbar setItems: buttonArray];
    [doneButton release];
    [flexibleSpace release];
    [self.view addSubview: toolbar];
}
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem 
{
    NSMutableArray *itemsArray = [toolbar.items mutableCopy];
    [itemsArray insertObject: barButtonItem atIndex: 0];
    [toolbar setItems:itemsArray animated:NO];
}
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem 
{
    NSMutableArray *itemsArray = [toolbar.items mutableCopy];
    [itemsArray removeObject:barButtonItem];
    [toolbar setItems:itemsArray animated:NO];
    [itemsArray release];
}

3 个答案:

答案 0 :(得分:0)

我从一段时间的工作中认识到这个问题。我认为这只是偶尔发生的,我不记得我们是否真的修复了它。

在您的情况下,似乎更容易使用导航栏并设置rightBarButtonItemleftBarButtonItem。这应该可以解决你的问题。

答案 1 :(得分:0)

事实上,如果您将视图控制器放在UINavigationController中,您将获得UINavigationBar和导航控制器的所有功能,如果您选择使用它。

答案 2 :(得分:0)

问题是,当iPad旋转到横向模式时,我没有正确调整工具栏的大小。通过添加以下代码解决了问题:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
    [toolbar setFrame: CGRectMake( 0, 0, 700, 44 )];
}
else {
    [toolbar setFrame: CGRectMake( 0, 0, 768, 44 )];
}

return YES;
}
相关问题