这是我的代码。在尝试解决此问题一两个小时后,它开始显得拥挤。我尝试将启用的用户交互设置为“是”,我尝试仅使用一个按钮,而不使其成为另一个视图的子视图。
现在,我有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];
}
最佳答案
您正在添加filterBar
作为navigationBar
的子视图,但是您将navigationBar
中的y-origin设置为42。然后将filterButtonContainer
中的filterBar
的y-origin设置为2。假设navigationBar
的高度是44,大多数filterBar
和您的filterButtonContainer
和filterButton
的全部都不会在其navigationBar
super 视图之内,因此无法选择该按钮。
编辑:为了使其在保持定位不变的情况下是可选的,我建议将过滤器栏添加到self.view
。例如,更改:
[[[self navigationController] navigationBar] addSubview:filterBar];
至
[self.view addSubview:filterBar];