Move UIView up when the keyboard appears in iOS

后端 未结 18 1833
再見小時候
再見小時候 2020-11-28 20:38

I have a UIView, it is not inside UIScrollView. I would like to move up my View when the keyboard appears. Before I tried to use this solution: How can I make a UITextField

18条回答
  •  被撕碎了的回忆
    2020-11-28 20:57

    Use the following code in order to show and hide the keyboard

    //Declare a delegate, assign your textField to the delegate and then include these methods
    
    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
        return YES;
    }
    
    
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    
        [self.view endEditing:YES];
        return YES;
    }
    
    
    - (void)keyboardDidShow:(NSNotification *)notification
    {
        // Assign new frame to your view 
        [self.view setFrame:CGRectMake(0,-110,320,460)]; //here taken -110 for example i.e. your view will be scrolled to -110. change its value according to your requirement.
    
    }
    
    -(void)keyboardDidHide:(NSNotification *)notification
    {
        [self.view setFrame:CGRectMake(0,0,320,460)];
    }
    

提交回复
热议问题