如何隐藏键盘触摸滚动视图背景?

时间:2013-05-17 10:04:12

标签: iphone ios objective-c ios6

我经历了所有解决方案,但没有一个正在运作。我正在开发iOS 6的应用程序,ipad。当用户触摸外面(在scrollview上)时,我希望键盘消失......

8 个答案:

答案 0 :(得分:6)

在viewDidLoad中提供以下代码

-(void) ViewDidLoad
{
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self     action:@selector(tapped)];
tapScroll.cancelsTouchesInView = NO;
[scrollview addGestureRecognizer:tapScroll];
}

定义函数如下

- (void) tapped
{
[self.view endEditing:YES];
}

答案 1 :(得分:1)

尝试使用这个。我希望它会有所帮助。

  - (void)viewDidLoad
    {
        [super viewDidLoad];

        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)];
        gestureRecognizer.delegate = self;
        [scrollView addGestureRecognizer:gestureRecognizer];
    }

    -(void) hideKeyBoard:(UIGestureRecognizer *) sender
    {
        [self.view endEditing:YES];
    }

答案 2 :(得分:1)

将scrollview Delegate设置为self

self.scrollView.delegate=self;

然后

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{


if (sTitle.isFirstResponder) {
    [sTitle resignFirstResponder];
}
}

答案 3 :(得分:1)

您可以使用此代码隐藏键盘:

-(void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideTheKeyBoard:)];
    gestureRecognizer.delegate = self;
    [scrollView addGestureRecognizer:gestureRecognizer];
}

-(void) hideTheKeyBoard:(UIGestureRecognizer *)sender
{
    [self.view endEditing:YES];
}

答案 4 :(得分:0)

  UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
   [tapRecognizer setNumberOfTapsRequired:1];
   [tapRecognizer setDelegate:self];
   [scrollview addGestureRecognizer:tapRecognizer];


-(void)tapped:(id)sender {
      [textField resignFirstResponder];
      //    your code what you want
 }

答案 5 :(得分:0)

如果您使用xib,则只需将tapRecognizerscrollview相关联,然后将tapRecognizer's选择器事件创建为[self.view endEditing:YES]

答案 6 :(得分:0)

add custom button on scrollview and
-(IBAction)btn:(id)sender
{
  [txt resignfirstresponder];
}

enjoy it!!

答案 7 :(得分:0)

UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self     action:@selector(Click)];
ScrollClick.cancelsTouchesInView = NO;
[YOUR scrollview addGestureRecognizer:ScrollClick];


- (void)Click
{
 [self.view endEditing:YES];
}

试试这个。我希望它对你有所帮助。享受它!