第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

當(dāng)鍵盤(pán)出現(xiàn)時(shí) - 如何開(kāi)始編輯時(shí),如何讓UITextField向上移動(dòng)?

當(dāng)鍵盤(pán)出現(xiàn)時(shí) - 如何開(kāi)始編輯時(shí),如何讓UITextField向上移動(dòng)?

iOS
Qyouu 2019-05-25 16:14:09
當(dāng)鍵盤(pán)出現(xiàn)時(shí) - 如何開(kāi)始編輯時(shí),如何讓UITextField向上移動(dòng)?使用iOS SDK:我有一個(gè)UIView帶有UITextField鍵盤(pán)的s。我需要它能夠:UIScrollView一旦鍵盤(pán)出現(xiàn),允許滾動(dòng)內(nèi)容以查看其他文本字段自動(dòng)“跳躍”(通過(guò)向上滾動(dòng))或縮短我知道我需要一個(gè)UIScrollView。我已經(jīng)嘗試將我的類更改UIView為a UIScrollView但我仍然無(wú)法向上或向下滾動(dòng)文本框。我需要a UIView和a UIScrollView嗎?一個(gè)人進(jìn)去另一個(gè)嗎?需要實(shí)現(xiàn)什么才能自動(dòng)滾動(dòng)到活動(dòng)文本字段?理想情況下,盡可能多的組件設(shè)置將在Interface Builder中完成。我只想編寫(xiě)需要它的代碼。注意:我正在使用的UIView(或UIScrollView)是由tabbar(UITabBar)啟動(dòng)的,它需要正常運(yùn)行。編輯:我正在添加滾動(dòng)條,僅用于鍵盤(pán)出現(xiàn)時(shí)。即使它不需要,我覺(jué)得它提供了更好的界面,因?yàn)橛脩艨梢詽L動(dòng)和更改文本框。我已經(jīng)讓它工作,我改變UIScrollView了鍵盤(pán)上下的框架大小。我只是使用:-(void)textFieldDidBeginEditing:(UITextField *)textField {      //Keyboard becomes visible     scrollView.frame = CGRectMake(scrollView.frame.origin.x,                       scrollView.frame.origin.y, scrollView.frame.size.width,scrollView.frame.size.height - 215 + 50);                         //resize}-(void)textFieldDidEndEditing:(UITextField *)textField {    //keyboard will hide     scrollView.frame = CGRectMake(scrollView.frame.origin.x,         scrollView.frame.origin.y,       scrollView.frame.size.width,       scrollView.frame.size.height + 215 - 50); //resize}但是,這不會(huì)自動(dòng)“向上移動(dòng)”或?qū)⒖梢?jiàn)區(qū)域中的下部文本字段居中,這是我真正想要的。
查看完整描述

4 回答

?
狐的傳說(shuō)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

實(shí)際上,最好只使用Apple的實(shí)現(xiàn),如文檔中所提供的那樣。但是,他們提供的代碼是錯(cuò)誤的。將keyboardWasShown:評(píng)論下方的部分替換為以下內(nèi)容:

NSDictionary* info = [aNotification userInfo];

CGRect keyPadFrame=[[UIApplication sharedApplication].keyWindow convertRect:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] fromView:self.view];

CGSize kbSize =keyPadFrame.size;

CGRect activeRect=[self.view convertRect:activeField.frame fromView:activeField.superview];

CGRect aRect = self.view.bounds;

aRect.size.height -= (kbSize.height);


CGPoint origin =  activeRect.origin;

origin.y -= backScrollView.contentOffset.y;

if (!CGRectContainsPoint(aRect, origin)) {

    CGPoint scrollPoint = CGPointMake(0.0,CGRectGetMaxY(activeRect)-(aRect.size.height));

    [backScrollView setContentOffset:scrollPoint animated:YES];

}

Apple代碼的問(wèn)題是這些:(1)它們總是計(jì)算點(diǎn)是否在視圖的框架內(nèi),但它是a ScrollView,所以它可能已經(jīng)滾動(dòng),你需要考慮該偏移:


origin.y -= scrollView.contentOffset.y

(2)他們將contentOffset移動(dòng)鍵盤(pán)的高度,但我們想要相反(我們想要移動(dòng)contentOffset屏幕上可見(jiàn)的高度,而不是不是):


activeField.frame.origin.y-(aRect.size.height)


查看完整回答
反對(duì) 回復(fù) 2019-05-25
?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

textFieldDidBeginEdittingtextFieldDidEndEditing通話功能[self animateTextField:textField up:YES],如下所示:

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self animateTextField:textField up:YES]; }- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self animateTextField:textField up:NO];}-(void)animateTextField:(UITextField*)textField up:(BOOL)up{
    const int movementDistance = -130; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? movementDistance : -movementDistance); 

    [UIView beginAnimations: @"animateTextField" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];}

我希望這段代碼可以幫到你。

在Swift 2中

func animateTextField(textField: UITextField, up: Bool) {
     let movementDistance:CGFloat = -130
     let movementDuration: Double = 0.3

     var movement:CGFloat = 0
     if up 
     {
         movement = movementDistance     }
     else 
     {
         movement = -movementDistance     }
     UIView.beginAnimations("animateTextField", context: nil)
     UIView.setAnimationBeginsFromCurrentState(true)
     UIView.setAnimationDuration(movementDuration)
     self.view.frame = CGRectOffset(self.view.frame, 0, movement)
     UIView.commitAnimations()}func textFieldDidBeginEditing(textField: UITextField) {
    self.animateTextField(textField, up:true)}func textFieldDidEndEditing(textField: UITextField) {
    self.animateTextField(textField, up:false)}

SWIFT 3

 func animateTextField(textField: UITextField, up: Bool)
    {
        let movementDistance:CGFloat = -130
        let movementDuration: Double = 0.3

        var movement:CGFloat = 0
        if up        {
            movement = movementDistance        }
        else
        {
            movement = -movementDistance        }
        UIView.beginAnimations("animateTextField", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration)
        self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
        UIView.commitAnimations()
    }


    func textFieldDidBeginEditing(textField: UITextField)
    {
        self.animateTextField(textField: textField, up:true)
    }

    func textFieldDidEndEditing(textField: UITextField)
    {
        self.animateTextField(textField: textField, up:false)
    }


查看完整回答
反對(duì) 回復(fù) 2019-05-25
  • 4 回答
  • 0 關(guān)注
  • 711 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)