ios - 我的 UIView 有一个 UIButton 作为它的 subview 之一,但不响应 Objective-C 中的事件

标签 ios objective-c cocoa-touch uiview uibutton

这是我的代码。在尝试解决这个问题一两个小时后,它开始看起来很拥挤。我尝试将启用的用户交互设置为"is",我尝试过仅使用一个按钮而不将其设为另一个 View 的 subview 。

现在我有filterBar > filterBarContainer > filterButton。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.userInteractionEnabled = YES;
    self.view.userInteractionEnabled = YES;
    // Create filter bar with specified dimensions
    UIView *filterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 42, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
    [filterBar setUserInteractionEnabled:YES];

    // Make it a subview of navigation controller
    [[[self navigationController] navigationBar] addSubview:filterBar];
    [filterBar setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.9f]];
    [filterBar setTag:1];

    // Reusable vars
    // CGFloat filterBarX = filterBar.frame.origin.x;
    //CGFloat filterBarY = filterBar.frame.origin.y;
    CGFloat filterBarHeight = filterBar.frame.size.height;
    CGFloat filterBarWidth = filterBar.frame.size.width;

    // Create centre filter button with specified dimensions
    UIView *filterButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 2, filterBarWidth / 2 - 30 , filterBarHeight - 10)];
        [filterButtonContainer setUserInteractionEnabled:YES];

    // Make it a subview of filter bar
    [filterBar addSubview:filterButtonContainer];
    [filterButtonContainer setBackgroundColor:[UIColor redColor]];

    // Centre the button
    [filterButtonContainer setCenter:[filterBar convertPoint:filterBar.center fromView:filterBar.superview]];

     // Add button to filter button
    UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [filterButton setUserInteractionEnabled: YES];
    [filterButton setFrame:CGRectMake(0, 0,40 , 20)];
    [filterButton setTitle:@"test" forState:UIControlStateNormal];
    [filterButtonContainer addSubview:filterButton];
    [filterButton setBackgroundColor:[UIColor yellowColor]];
   // self.filterButton = filterButton;
    [filterButton addTarget:self action:@selector(filterButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"dd");
    filterButtonContainer.layer.borderColor = [UIColor blackColor].CGColor;
    filterButtonContainer.layer.borderWidth = 1;

这是我尝试触发的方法。

- (void)filterButtonTapped:(UIButton *)sender
{
    NSLog(@"filter button tapped");
    UIActionSheet *filterOptions = [[UIActionSheet alloc] initWithTitle:@"Filter Garments"
                                                              delegate:self
                                                     cancelButtonTitle:@"Cancel"
                                                destructiveButtonTitle:nil
                                                      otherButtonTitles:@"Recommended", @"What's New", @"Price - High to Low", @"Price - Low to High", nil];
    [filterOptions showInView:self.collectionView];

}

最佳答案

您的父 View (按钮的 super View )很可能具有将过滤器按钮置于其区域之外的宽度高度。该按钮仍然显示(如果您没有定义 View 应切断 subview ),但 View 区域之外的元素将不会获得任何触摸事件。检查您的 super View 的宽度和高度。

关于ios - 我的 UIView 有一个 UIButton 作为它的 subview 之一,但不响应 Objective-C 中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23009873/

相关文章:

ios - Google 广告未在特定应用中展示

objective-c - Xcode 的多合一共享框架

ios - 将 TableView 添加为 subview 不会重新加载数据

ios - 在 Swift 中实例化带有标识符的 View Controller

objective-c - 获取当前时间

ios - 如何在 iOS 中获取所有应用程序的 ViewControllers?

iphone - 在 iOS SDK 上将实际日期转换为秒

objective-c - 从 AppDelegate 更改选项卡和推送 View

ios - 将 UICollectionViewCell 滚动到背景而不是垂直

ios - 带有 tableView 按钮的交互式标题