2016-04-14 43 views
1

Klavye sunulduğunda UItextView değişiklik kısıtlamalarının altına sahip olmaya çalışıyorum, böylece UItextView metinView tarafından "kısıtlanmış" olur. UIView var. UItextView ve alt düzen kılavuzu arasında boşluk ekleyici var. Ara parçası için kısıtlamanın değerini, görünümün altına klavye boyuna değiştirmeye çalışıyorum. Herhangi bir yardım takdir edilir. Ben bunu çok daha basit dibini ayarlamak için bulmakSwift: Klavye görüntülendiğinde otomatik düzenleme kısıtlamalarını değiştir

Şahsen
func keyboardShown(notification: NSNotification) { 

    let info = notification.userInfo! 

    let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]! 

    let rawFrame = value.CGRectValue 

    keyboardFrame = view.convertRect(rawFrame, fromView: nil) 

    print(keyboardFrame) 

    print(spacerBottomLayoutConstraint) 

    //Setting text field to bottom of keyboard 
    //spacerBottomLayoutConstraint.constant = keyboardFrame.height 

    UIView.animateWithDuration(1, animations: { 

     self.spacerBottomLayoutConstraint.constant = self.keyboardFrame.height 

     self.view.layoutIfNeeded() 
    }) 
} 
+0

Ve sorun nedir? Kodunu görüyorum ve hedefini görüyorum ama bir soru göremiyorum. (Bu arada, canlandırmaya gerek yok; klavyeyle birlikte otomatik animasyon elde edersiniz.) – matt

+0

Evet mat doğru –

+0

Klavyenin 'UITextView' öğesini basmasını ve 'UIView' ara parçasının ara belleğini aralarında bulundurmasını istiyorum. . Şu anda metin alanı klavyenin arkasında. – Manstof

cevap

7

: sihir

textView.becomeFirstResponder() 

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AdditionalDetailsVC.keyboardShown(_:)), name: UIKeyboardDidShowNotification, object: nil) 

ve:

@IBOutlet weak var textView: UITextView!  
@IBOutlet weak var spacer: UIView! 
@IBOutlet weak var spacerBottomLayoutConstraint: NSLayoutConstraint! 

viewDidLoad yılında: Burada

benim kodudur metin görünümünün içeriği. Bu kod, tüm pencereyi kaplayan bir metin görünümü içindir (alt kısmı ekranın altıdır);

func keyboardShown(n:NSNotification) { 
    let d = n.userInfo! 
    var r = (d[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 
    r = self.textView.convertRect(r, fromView:nil) 
    self.textView.contentInset.bottom = r.size.height 
    self.textView.scrollIndicatorInsets.bottom = r.size.height 
} 
+1

Swift 3'te, 'CGRectValue()' 'cgRectValue' –

+1

@JasonMoore Swift 3'te doğrudan CGRect'e yazabilir ve NSValue’yi tamamen atlayabilirsiniz. Kodun güncellenmiş sürümü şuradadır: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p522textFieldScrollView/ch23p805textFieldSliding/ViewController.swift – matt

+1

Sweet! Yani 'd [UIKeyboardFrameEndUserInfoKey] olarak! CGRect, bunun karşısında tökezleyen herkes için. –

2

görüntülemeye dibine kadar aralama için kısıtlama değerini değiştirmek için aşağıdaki kodu deneyebilirsiniz: uzakta biraz daha temel aritmetik gerektirdiğini inmiyor bir metin görünümü klavye yüksekliği. Aşağıdaki kodu ViewController'ınıza yerleştirin.

// MARK: Keyboard Event Notifications 

func handleKeyboardNotification(notification: NSNotification) { 
    let userInfo = notification.userInfo! 
    print(notification) 
    print(notification.object) 

    // Get information about the animation. 
    let animationDuration: NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue 

    let rawAnimationCurveValue = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).unsignedLongValue 
    let animationCurve = UIViewAnimationOptions(rawValue: rawAnimationCurveValue) 

    // Convert the keyboard frame from screen to view coordinates. 
    let keyboardScreenBeginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue() 
    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 

    let keyboardViewBeginFrame = view.convertRect(keyboardScreenBeginFrame, fromView: view.window) 
    let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window) 
    print(keyboardViewBeginFrame) 
    print(keyboardViewEndFrame) 

    // Determine how far the keyboard has moved up or down. 
    let originDelta = keyboardViewEndFrame.origin.y - keyboardViewBeginFrame.origin.y 
    print(originDelta) 

    // Adjust the table view's scroll indicator and content insets. 
    textView.scrollIndicatorInsets.bottom -= originDelta 
    textView.contentInset.bottom -= originDelta 

    print(keyboardViewEndFrame) 

    spacerBottomLayoutConstraint?.constant = CGFloat(originDelta) 

    // Inform the view that its the layout should be updated. 
    [self.view setNeedsLayout]; 

    // Animate updating the view's layout by calling layoutIfNeeded inside a UIView animation block. 
    let animationOptions: UIViewAnimationOptions = [animationCurve, .BeginFromCurrentState] 
    UIView.animateWithDuration(animationDuration, delay: 0, options: animationOptions, animations: { 
     self.view.layoutIfNeeded() 
     }, completion: nil) 
}