1

我正在尝试将 UIView 作为附件视图添加到 UITextField,但似乎不尊重 alpha 属性。

这是我当前的代码。

self.keyboardAccView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
[self.keyboardAccView setBackgroundColor:[UIColor lightGrayColor]];
[self.keyboardAccView setOpaque:NO];
[self.keyboardAccView setAlpha:0.0];

UITapGestureRecognizer *hideKeyboardTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)];
[self.keyboardAccView addGestureRecognizer:hideKeyboardTap];
[hideKeyboardTap release], hideKeyboardTap=nil;

alpha 值似乎无关紧要。无论我将其设置为什么,accessoryView 始终设置为不透明。

我想要完成的是在键盘上方显示一个透明视图,只要用户从键盘上轻按,它就会关闭键盘。如果有更好/正确的方法来做到这一点,我完全错过了,我也会全神贯注。

  • 编辑 *

我知道我可以只[UIColor clearColor]用作背景颜色,但我更想知道为什么不支持 alpha 设置,以防我真的想要一个半透明的附件视图

4

1 回答 1

0

是的 - 有更好的方法来做到这一点!:)
我个人是这样做的——我改变了我的 ViewController.xib 类(UIView 到 UIControl),然后我创建了一个简单的 IBAction,只要 UIControl 被点击,它就会让第一响应者辞职。

该方法应如下所示:

- (IBAction)hideKeyboard {
[textField resignFirstResponder];
}

而已 :)

于 2011-12-09T20:03:39.287 回答