表格页脚视图中的UIButton在键盘显示时没有响应触摸

问题描述 投票:1回答:3

我正在开发一个带有购物车的iPhone应用程序,我正在使用UITableView来显示购物车。每个项目都有一个单元格,-tableFooterView设置为自定义视图,为用户提供文本字段以验证其信用卡的CVV以及完成结帐流程的按钮。

当用户点击CVV文本字段时,我调整表格视图的大小,以便键盘不会覆盖任何内容。

- (void)keyboardWillShow:(NSNotification *)n
{   
    // I'll update this to animate and scroll the view once everything works 
    self.theTableView.frameHeight = self.view.frameHeight - KEYBOARD_HEIGHT_PORTRAIT_IPHONE;
}

输入CVV后,用户可以点击完成键以关闭键盘:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

但是,所有这些都可以,当键盘可见时,我的结帐按钮(普通UIButton)不响应触摸事件。该表滚动,但按钮的touchUpInside事件永远不会被触发。

一旦我点击完成并且键盘被解除,结账按钮将识别touchUpInside事件。

从我所看到的情况来看,键盘覆盖的任何按钮似乎都不响应我的触摸(即使它从键盘后面向外滚动),直到键盘被解除。当键盘可见时,键盘未覆盖的同一-tableFooterView中的按钮仍保持对触摸的响应。

在iOS 5和iOS 4上运行时的行为相同。

任何人都可以对可能发生的事情提出任何建议吗?或者任何有用的故障排除方法?

谢谢!

编辑 - 更新

事实上,键盘覆盖的tableFooterView部分没有响应触摸事件。在我的自定义UIView子类中,我实现了-touchesBegan:withEvent:并只记录发生了触摸。

触摸视图中的任何位置都会在显示键盘之前获取日志语句。但是,在调整tableview的大小后,只触摸视图的上半部分会生成一个日志语句。

我刚刚意识到,一旦我滚动该部分可见,键盘覆盖的tableFooterView部分将变为包含视图背景颜色的颜色。

iphone ios uitableview uiview uiscrollview
3个回答
1
投票

我有同样的问题。我认为这是iOS中的一个错误,但我发现了一个解决方法:


- (void)keyboardWillShow:(NSNotification *)note {
    NSDictionary* userInfo = [note userInfo];

    // get the size of the keyboard
    NSValue *boundsValue = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [boundsValue CGRectValue].size; // screen size
    CGFloat keyboardHeight;
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || 
        self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        keyboardHeight = keyboardSize.height;
    } else {
        keyboardHeight = keyboardSize.width;
    }

    // resize the view with the keyboard
    __block CGRect origFrame = self.view.frame;
    __block CGRect viewFrame = origFrame;
    viewFrame.size.height -= keyboardHeight - ((self.tabBarController != nil) ? self.tabBarController.tabBar.frame.size.height : 0);
    // Set the height to zero solves the footer view touch events disabled bug
    self.view.frame = CGRectMake(origFrame.origin.x, origFrame.origin.y,
                                 viewFrame.size.width, 0);
    // We immediately set the height back in the next cycle, before the animation starts
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        self.view.frame = origFrame; // The original height
        // start the animation
        [UIView animateWithDuration:0.4 animations:^{
            [self.view setFrame:viewFrame];
        }];
    });
}

诀窍是将tableView的高度设置为0并在下一个运行周期中恢复为原始高度。

这适用于iOS 4.3和5.1。


0
投票

我认为调整UITableView的大小会使它将UIButton(子视图)发送到视图层次结构的后面。在调整框架大小后,您可能需要明确地将它带到前面。

[self.theTableView bringSubviewToFront:yourUIButton];

0
投票

以下为我工作。

有一个带有按钮的表格视图页脚,因为该按钮操作通过xib链接,除了添加以下代码更多 -

@IBOutlet private weak var myButton: CustomUIButton!

override public func awakeFromNib() {
    super.awakeFromNib()

    let myButtonTapGesture = UITapGestureRecognizer(target: self, action: #selector(myButtonAction(_:)))
    myButton.addGestureRecognizer(myButtonTapGesture)
}

@IBAction func myButtonAction(_ sender: AnyObject) {
    // button action implementation
}

所以我必须在按钮上添加一个轻击手势。

© www.soinside.com 2019 - 2024. All rights reserved.