2015-04-04 38 views
2

我有一個帶有UITextField的UIToolBar以及一個標籤。我試圖讓用戶輸入時更新標籤,以便知道他們鍵入了多少個字符。UIToolBar在更改標籤後恢復

當我試着更新標籤計數器時,UIToolBar當前返回到它的原始位置。 Here is a gif showing the issue I'm having.

我做的是以下幾點:

-(IBAction)CharCount:(id)sender{ 
    NSString *substring = textField.text; 
    NSString *limitHit; 
    limitHit = substring; 
    int maxChar = 160; 
    if (limitHit.length > 0) { 
     TextCounter.hidden = NO; 
     TextCounter.text = [NSString stringWithFormat:@"%d/160", limitHit.length]; 
    } 
} 

我怎麼會去更新標籤無需反轉動畫與鍵盤一起移動工具欄?編號======================== ===

不使用自動佈局意味着我在iPhone 4S上的視圖是錯誤的。下面是他們的一個例子。底部的菜單掛起。我該如何設置它纔不會發生?

example iPhone 4S

+0

這是因爲自動佈局。當你使用它時,你應該不**設置任何框架。您需要通過修改其約束來重新定位工具欄,而不是設置框架。 – rdelmar 2015-04-04 04:51:22

+2

「這個問題還沒有得到足夠的重視」相反,@ rdelmar已經正確地告訴了你答案。問題是你沒有聽。你無緣無故地拋棄了你的名聲。這個問題(關於更新文本字段時在autolayout下發生了什麼)已經在Stack Overflow上多次得到解答。 – matt 2015-04-15 19:23:02

+0

我知道他的迴應,並且我非常感謝他們。如上所述,我編輯了這個問題,重點關注所提供信息的問題。我一直在尋找幾個小時試圖找到解決方案,但還沒有找到它。因此,我提供了賞金。請隨時鏈接提供我需要的信息的問題和答案。 – Jahoe 2015-04-15 19:36:08

回答

1

不要關閉自動佈局,只需更改約束而不是幀。由於layoutSubviews方法的原因,使用自動佈局更改框架不起作用。這種方法在很多情況下被系統調用。您需要:

  1. 添加底部約束到工具欄:

    enter image description here

  2. 訂閱鍵盤的通知。

  3. 當鍵盤顯示或隱藏時,更改工具欄的底部約束。

代碼示例:

- (void)dealloc { 
    [self unsubscribeForKeyboardNotifications]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self subscribeForKeyboardNotifications]; 
} 

#pragma mark - Keyboard notifications 

- (void)subscribeForKeyboardNotifications { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillAppear:) 
               name:UIKeyboardWillShowNotification 
               object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillDisappear:) 
               name:UIKeyboardWillHideNotification 
               object:nil]; 

} 

- (void)unsubscribeForKeyboardNotifications { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)keyboardWillAppear:(NSNotification *)notification { 
    CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; 
    [self changeToolbarBottomConstraintWithConstant:keyboardHeight]; 
} 

- (void)keyboardWillDisappear:(NSNotification *)notification { 
    [self changeToolbarBottomConstraintWithConstant:0]; 
} 

- (void)changeToolbarBottomConstraintWithConstant:(CGFloat)constant { 
    [self.toolBar.superview.constraints enumerateObjectsUsingBlock: 
      ^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) { 
       if (constraint.secondItem == self.toolBar && constraint.secondAttribute == NSLayoutAttributeBottom) 
        constraint.constant = constant; 
      }]; 
    [UIView animateWithDuration:0.5 
        animations:^{ 
         [self.view layoutIfNeeded]; 
        }]; 
} 

結果:

enter image description here

1

這每一部分看起來像它可以簡化並通過設置UIToolbar爲解決UITextviewinputAccessoryView。這會在工具欄上下移動時將工具欄附加到鍵盤上。如果你想讓它留在視圖控制器視圖的底部,您可以覆蓋視圖控制器的inputAccessoryView,然後將這個方法添加到您的視圖控制器的實現文件:

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

Here是一個方便介紹在視圖控制器上使用inputAccessoryView。

1

無需拆下自動佈局僅僅增加兩個約束尾隨空間toolbarview和修復寬度約束 希望這將有助於你有類似的問題,我以這種方式解決。

1

您也可以通過設置框架而無需自動佈局。在名爲InputView的視圖中添加textField和label並將其添加到自身中。查看和textField作爲tfInput

現在在您的視圖控制器中設置文本字段的委託。

然後,只需根據需要更改Y視圖的位置。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
     if(textField== tfInput) 
     { 
      InputView.frame = CGRectMake(InputView.frame.origin.x,self.view.frame.size.height - 216 - InputView.frame.size.height,InputView.frame.size.width,InputView.frame.size.height); 
     } 
     return YES; 

} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
      if(textField== tfInput) 
      { 
       InputView.frame = CGRectMake(InputView.frame.origin.x,self.view.frame.size.height - 49 InputView.frame.size.height,InputView.frame.size.width,InputView.frame.size.height); 
      } 
      return YES; 
} 

這裏我設置49工具欄的大小,它可以由您自定義尺寸。 也可以在框架設置時做一些動畫。

這是一個框架集合的選項。

第二個選項是放在滾動視圖和相同的文本字段委託方法textFieldShouldBeginEditing你必須設置內容偏移到你需要的地方,並使其在textFieldShouldReturn中爲0。