【问题标题】:Can't click bar button on Tool bar?无法单击工具栏上的栏按钮?
【发布时间】:2013-02-12 05:06:31
【问题描述】:

在我的 iPad 应用程序中,我手动添加导航栏和工具栏。
我还通过编码添加滚动视图和图像视图。

我的问题是我可以同时看到导航栏和工具栏。
我可以点击导航栏上的按钮。
但是,我无法单击工具栏上的栏按钮。

我该怎么做?

这是我的代码。

- (void)viewDidLoad
{

[super loadView];
self.view.backgroundColor = [UIColor grayColor];

UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;

NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat xOrigin = i * self.view.frame.size.width;

    // Create a UIImage to hold Info.png
    UIImage *image1 = [UIImage imageNamed:@"Image-001.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"Image-002.jpg"];
    UIImage *image3 = [UIImage imageNamed:@"Image-003.jpg"];
    UIImage *image4 = [UIImage imageNamed:@"Image-004.jpg"];

    NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,nil];

    UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)];
    [ImageView setImage:[images objectAtIndex:i]];

    [ScrollView addSubview:ImageView];
}

ScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:ScrollView];
}

【问题讨论】:

  • 不是你贴的代码,而是工具栏相关的代码。
  • @rmaddy,很抱歉我的问题不完整,我手动添加了工具栏。然后我也手动在此工具栏上添加了三个按钮。 (不是来自代码)。我将这个按钮与它们相关的视图控制器连接起来。如果我单击按钮 A,它将与视图控制器 A 连接。(我将它们与 segue - modal 连接)。我可以看到我的工具栏,但我不能点击它上面的按钮。那是我的问题。我能解决吗?
  • 看起来你添加的滚动视图覆盖了除了视图控制器视图的最顶部之外的所有内容。滚动视图位于工具栏的顶部。这将阻止任何点击事件进入工具栏。如果您在 Interface Builder 中添加了工具栏和导航栏,为什么不在 Interface Builder 中添加滚动视图呢?然后确保它不与工具栏重叠。
  • @rmaddy,感谢您的帮助。请让我问一个问题。我可以减少滚动视图的上部。所以,我可以点击导航栏上的按钮。我怎样才能减少滚动视图的下部?
  • 正确设置滚动视图的框架。它需要小于视图控制器的视图大小。您还需要确保正确设置滚动视图的autoresizingMask

标签: ios ipad cocoa-touch


【解决方案1】:

我不知道您的确切问题是什么,因为您显示的代码与UIToolBar 无关。

但在下面的代码中,我添加了两个带有 flexSpaceUIBarButton
您可以根据需要添加 UIBarButton

UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    Toolbar = UIBarStyleBlackTranslucent;
    [Toolbar sizeToFit];

     NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    [flexSpace release];

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(Cancel)];
    [barItems addObject:btnCancel];

    UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done)];
    [barItems addObject:btnDone];

    [Toolbar setItems:barItems animated:YES];

点击条形按钮时调用以下方法

-(void)Cancel
{
  // Write Code for Cancel Method
}

-(void)done
{
  // Write Code for Done Method
}

此代码可能对您自己的情况有所帮助。

谢谢:)

【讨论】:

  • 很抱歉我的问题不完整,我手动添加了工具栏。然后我也手动在此工具栏上添加了三个按钮。 (不是来自代码)。我将这个按钮与它们相关的视图控制器连接起来。如果我单击按钮 A,它将与视图控制器 A 连接。(我将它们与 segue - modal 连接)。我可以看到我的工具栏,但我不能点击它上面的按钮。那是我的问题。我能解决吗?
【解决方案2】:

谢谢大家。我可以解决我的问题。
我改变我的代码。

UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];

到这个。

UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];

【讨论】:

  • 您不应硬编码 88 值。您应该获得导航栏和工具栏的实际高度。并且不要忘记设置滚动视图的autoresizingMask。否则,如果视图控制器的大小发生变化(例如在横向旋转/从横向旋转时),滚动视图的大小不会改变。
  • @rmaddy,谢谢。你真的帮了我。我计划旋转我的视图方向。我忘了考虑方向。你的指导方针对我很重要。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2014-12-22
  • 2019-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
相关资源
最近更新 更多