【问题标题】:iOS- Change of UITextField height and movement of Buttons based on keyboard displayiOS-基于键盘显示的UITextField高度和按钮移动的变化
【发布时间】:2013-12-11 13:19:25
【问题描述】:

我在 iOS 中有一个视图,其中 UITextField 下方有一个 UITextfield 和 3 个 UIbuttons。请看下面的图片。启动此视图时,默认状态为 STATE1。(请参见图片)。默认情况下键盘是可见的。现在,当我处理键盘时,我希望调整编辑文本的大小并占据整个屏幕,如 STATE2 所示。

我不知道如何做到这一点。我将 UITextfield 的高度硬编码为基于目标设备的某个 dp。我相信这必须改变,它必须根据屏幕尺寸动态地占据屏幕。

谁能帮我完成这个。请考虑该按钮就像编辑文本的尾巴。无论键盘是否可见,这都会紧贴编辑文本。谢谢

【问题讨论】:

    标签: ios uitextfield


    【解决方案1】:

    您可以使用TPKeyboardAvoiding 库并实现您想要的。

    【讨论】:

    • 阅读。会回复你的。谢谢
    【解决方案2】:

    首先你必须使用 UITextView 而不是 UITextField

    为你的班级注册

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    

    实现这个方法

    - (void)keyboardWillHide:(NSNotification *)notification {
        [self resizeViewWithOptions:[notification userInfo]];
    }
    
    - (void)keyboardWillShow:(NSNotification *)notification  {
        [self resizeViewWithOptions:[notification userInfo]];
    }
    
    - (void)resizeViewWithOptions:(NSDictionary *)options {   
    
        NSTimeInterval animationDuration; 
        UIViewAnimationCurve animationCurve;
        CGRect keyboardEndFrame;
        [[options objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
        [[options objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
        [[options objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:animationCurve];
        [UIView setAnimationDuration:animationDuration];
        CGRect viewFrame = self.view.frame;
    
        CGRect keyboardFrameEndRelative = [self.view convertRect:keyboardEndFrame fromView:nil];
        NSLog(@"keyboardFrameEndRelative: %@", NSStringFromCGRect(keyboardFrameEndRelative));
    
    // Now set Frame for UITextView and UIbuttons frame here
    
    }
    

    【讨论】:

    • 嗨 Bhuvaneshwar,我们需要在哪里注册课程。你能告诉我吗。这里的 addObserver 是什么?谢谢
    • @TimothyRajan 在您的类本身中,您必须编写此方法。在viewdidLoad' and define this method in same class. Here addObserver 中注册您的键盘通知类作为目标。
    【解决方案3】:

    尝试这样更简单,首先添加那些你想要的控件,在状态 1 中看起来像这样

    在 viewDidAppear 中添加如下函数

    -(void)viewDidAppear:(BOOL)animated
    {
        [self.Textfield1 becomeFirstResponder];
    
    }
    

    所以它会在视图加载时加载键盘

    然后将此行添加到 .m 以关闭键盘按回车将隐藏键盘

    -(BOOL) textFieldShouldReturn:(UITextField *)textField{
    
        //Write your coding for resizing size dynamically to show look like state 2
        //Here check version of device to set as per height
        textfield.frame=CGRectMake(0,0,320,400);
        button1.frame=CGRectMake(0,400,320,400);
        [textField resignFirstResponder];
        return YES;
    
    }
    

    不要忘记为文本字段添加委托

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 2019-04-04
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      相关资源
      最近更新 更多