【问题标题】:iOS Drop down menu from right bar button don't get selected未选择右侧栏按钮的iOS下拉菜单
【发布时间】:2014-08-27 00:31:54
【问题描述】:

我使用了NIDropDown 示例,我想创建一个小菜单,当点击时它会从右侧导航栏下拉。我也看到了其他示例,但我不想填充整个控制器,当我只填充按钮下方的空间时,这对我来说就足够了,并且使用该组件中的示例真的很容易。问题是我无法选择显示表中的任何行,除了第一行。我想这是因为桌子的其余部分在导航栏之外,对吗?我该如何解决?我可以以某种方式扩展手势识别器的区域吗?还是应该在表格中添加新的识别器?

在我的控制器中,我有这些代码行用于创建右栏按钮并显示下拉列表:

- (void)setRightBarButtonItem
{
    UIButton *rightButton = [NavigationBarHelper getRightBarButtonItemForMenu];
    [rightButton addTarget:self action:@selector(rightBarButtonTapped:) forControlEvents:UIControlEventTouchDown];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)rightBarButtonTapped:(id)sender
{
    MAAppDelegate* appDelegate = (MAAppDelegate*)[[UIApplication sharedApplication] delegate];
    NSArray *menuItems = [appDelegate menuItems];
    NSArray *arrayOfNames = [menuItems valueForKey:@"text"];
    if(dropDown == nil) {
        CGFloat f = 320;
        dropDown = [[NIDropDown alloc]showDropDown:sender :&f :arrayOfNames :nil :@"down"];
        dropDown.delegate = nil;
        [_tableView setUserInteractionEnabled:NO];
    }
    else {
        [dropDown hideDropDown:sender];
        dropDown = nil;
        [_tableView setUserInteractionEnabled:YES];
    }
}

这里是showDropDown 方法:

- (id)showDropDown:(UIButton *)b:(CGFloat *)height:(NSArray *)arr:(NSArray *)imgArr:(NSString *)direction {
    btnSender = b;
    animationDirection = direction;
    self.table = (UITableView *)[super init];
    if (self) {
        // Initialization code
        CGRect btn = b.frame;
        self.list = [NSArray arrayWithArray:arr];
        self.imageList = [NSArray arrayWithArray:imgArr];
        if ([direction isEqualToString:@"up"]) {
            self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
            self.layer.shadowOffset = CGSizeMake(-5, -5);
        }else if ([direction isEqualToString:@"down"]) {
            self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
            self.layer.shadowOffset = CGSizeMake(-5, 5);
        }

        self.layer.masksToBounds = NO;
        self.layer.shadowRadius = 5;
        self.layer.shadowOpacity = 0.5;

        table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 140, 0)]; //btn.size.width
        table.delegate = self;
        table.dataSource = self;
        table.backgroundColor = kDarkGreen;
        table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        table.separatorColor = [UIColor grayColor];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        if ([direction isEqualToString:@"up"]) {
            self.frame = CGRectMake(btn.origin.x, btn.origin.y-*height, btn.size.width, *height);
        } else if([direction isEqualToString:@"down"]) {
            self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, 140, *height); //btn.size.width
        }
        table.frame = CGRectMake(0, 0, 140, *height); //btn.size.width
        [UIView commitAnimations];
        [b.superview addSubview:self];
        [self addSubview:table];
    }
    return self;
}

【问题讨论】:

    标签: ios uitableview drop-down-menu uinavigationbar uigesturerecognizer


    【解决方案1】:

    iOS 将允许您拥有一个在其父视图边界之外绘制的视图——假设您使用masksToBounds = NO 允许它。不幸的是,在视图边界之外绘制的任何内容都不会正确地拾取触摸。

    【讨论】:

    • 我有什么技巧可以检测到它吗?或者我必须为此使用另一种解决方案?奇怪的是,当我将 masksToBounds 设置为 YES 时,我仍然在导航栏外看到表格。
    • 如果你设置 self.masksToBounds = NO,那么所有的子视图也会被屏蔽。 (问题也可能来自您的 superview)。
    猜你喜欢
    • 1970-01-01
    • 2017-10-07
    • 2021-10-09
    • 1970-01-01
    • 2013-06-23
    • 2012-10-27
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多