这是我的代码。在尝试解决这个问题一两个小时后,它开始看起来很拥挤。我尝试将启用的用户交互设置为"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/