0

キーボードが表示されたら、UIVCollectionView の contentSize を追加し、上にスクロールできるようにしたかったのですが、機能しませんでした。彼はまだスクロール範囲を広げていませんでした。

contentSize を設定し self.collectionView.contentSize = CGSizeMake(0, self.view.frame.size.height + deltaY)ました。シミュレーター 5s Xcode7.1 でアプリを実行します。contentSize は (0.0, 832.0) です。UIcollectionView が上にスクロールできない理由。

これは、keyboardWillShow メソッドです。

func keyboardWillShow(notification: NSNotification) {

    if let userInfo = notification.userInfo {

        let keyboardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()


        let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue

        let keyboardBoundsRect = self.view.convertRect(keyboardBounds, toView: nil)

        let deltaY = keyboardBoundsRect.size.height + finishViewHeight

        print(self.collectionContentSize)

       //here I set contentSize
       self.collectionView.contentSize = CGSizeMake(0, self.view.frame.size.height + deltaY)

        print(self.collectionView.contentSize)

        let animations: (()->Void) = {

            self.finishView!.transform = CGAffineTransformMakeTranslation(0, -deltaY)
        }

        if duration > 0 {

            let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue << 16))

            UIView.animateWithDuration(duration, delay: 0, options:options, animations: animations, completion: nil)

        } else {

            animations()
        }


    }


}
4

1 に答える 1

0

この場合、コンテンツ インセットを設定UICollectionViewして、スクロール用のスペースを確保する必要があります。

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
    NSDictionary *info = aNotification.userInfo;
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    self.collectionView.contentInset = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);;
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification {
    self.collectionView.contentInset = UIEdgeInsetsZero;
}
于 2015-11-03T09:11:16.640 に答える