2013-02-09 20 views
7

UITextView, modal denetleyici görünümünün alt görünümüdür. UITextView'in alt kenarlık y koordinatının klavyenin üst y koordinatına eşit olması için klavye belirdiğinde UITextView yüksekliğini azaltmam gerekiyor. I'getting klavye yüksekliğiUIModalPresentationFormSheet. Klavye görüntülendiğinde UITextView yüksekliğinin ayarlanması

CGRect frameBegin = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] ; 
CGRect frameEnd = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 

CGRect resultBegin = [self.view convertRect:frameBegin fromView:nil]; 
CGRect resultEnd = [self.view convertRect:frameEnd fromView:nil]; 

CGFloat kbdHeight = resultBegin.origin.y - resultEnd.origin.y; 

sorun klavye göründüğünde bu modal görünümü yukarı atlar olmasıdır. Bu durumda klavyenin üst sınır koordinatı nasıl hesaplanır?

cevap

4

Bunu yapabilirsiniz:

1. Register for keyboard notifications: 

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

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(myTextViewHeightAdjustMethod:) 
              name:UIKeyboardDidShowNotification 
              object:nil]; 

2. Calculate intersection and adjust textView height with the bottom constraint 

    - (void)myTextViewHeightAdjustMethod:(NSNotification *)notification 
    { 
     NSDictionary *userInfo = [notification userInfo]; 
     CGRect keyboardFinalFrame = [[userInfo emf_ObjectOrNilForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 

     CGPoint keyboardOriginInView = [self.view convertPoint:keyboardFinalFrame.origin fromView:nil]; 

      CGFloat intersectionY = CGRectGetMaxY(self.view.frame) - keyboardOriginInView.y; 

      if (intersectionY >= 0) 
      { 
       self.textViewBottomConstraint.constant = intersectionY + originalTextViewBottomConstraint; 

       [self.textView setNeedsLayout]; 
    } 

bildirimleri için kaydını unutmayın.

+0

Soruyu oluşturduğumdan beri bir çok şey oldu. Her neyse cevabın için teşekkürler. – Michael

0

sana kullanılması önerilir bunun için kendinizi kod yazmak gerekmiyorsa https://github.com/hackiftekhar/IQKeyboardManager

O (şimdiye kadar, benim için) harika çalışıyor ve yapmanız gereken tüm ithal ve bu satırı ekleyin AppDelegate kodunda:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    //Magic one-liner 
    IQKeyboardManager.sharedManager().enable = true 

    return true 
} 
İlgili konular