2011-06-01 102 views
0

尝试在基于分割视图的应用程序中的detailviewcontroller工具栏的右侧添加按钮。我用灵活的空间把它放在右边。在肖像中,它可以很好地工作,但是在横向(当菜单按钮消失时),按钮会移动,因此其中一半离开了屏幕。如何在UISplitViewController工具栏的右侧添加一个按钮?

下面是相关的代码(在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]; 
} 
+1

这个问题很可能是由于autoresizing掩码。您可能会检查设置并确保它们是您的意思。 – PengOne 2011-06-01 21:47:17

+0

我过去也遇到过这个问题,并且我以某种方式修复了它,但是我的记忆就像是一个漏勺。 – 2011-06-01 21:47:40

+1

此外,您提出了14个问题并接受了0个答案。您可能希望回到以前的问题,并接受(单击旁边的复选标记)解决问题的答案。通过这样做,您成为SO社区的积极成员,并且更有可能鼓励人们花时间在未来尝试帮助您。 – PengOne 2011-06-01 21:49:13

回答

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; 
} 
0

我承认这个问题从一个项目我的工作而回。我认为这只是偶尔发生的,我不记得我们是否确实修复了它。

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

0

事实上,如果你把你的视图控制器在一个UINavigationController你得到的UINavigationBar的和导航控制器的所有功能,如果你选择使用它。

相关问题