2013-02-09 30 views
7

UITextView मोडल नियंत्रक दृश्य का सबव्यूव है। कीबोर्ड के शीर्ष वाई समन्वय के बराबर होने के लिए UITextView के नीचे सीमा वाई समन्वय के क्रम में कुंजीपटल दिखाई देने पर मुझे UITextView ऊंचाई कम करने की आवश्यकता है। I'getting कीबोर्ड ऊंचाईउमोडल प्रस्तुतिफॉर्मशीट। कीबोर्ड प्रकट होने पर UITextView ऊंचाई को समायोजित कैसे करें

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; 

समस्या यह है कि इस मॉडल को देखने कूदता है जब कुंजीपटल दिखाई देता है। इस मामले में कीबोर्ड के शीर्ष सीमा समन्वय की गणना कैसे करें?

उत्तर

4

आप ऐसा कर सकता है:

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]; 
    } 

सूचनाओं के लिए पंजीकरण रद्द करना न भूलें।

+0

प्रश्न बनाने के बाद से बहुत सी चीजें हुईं। वैसे भी जवाब के लिए धन्यवाद। – Michael

0

आप मैं का उपयोग करना चाहिये अपने आप को इस के लिए कोड लिखने की जरूरत नहीं है https://github.com/hackiftekhar/IQKeyboardManager

यह (मेरे लिए, अब तक) अच्छा काम करता है और तुम सब करने की जरूरत है इसे आयात और की इस एक लाइन जोड़ने है AppDelegate में कोड:

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

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

    return true 
}