2015-11-03 70 views
0

當鍵盤出現時,我添加了UIVCollectionView的contentSize,希望它能夠向上滾動但不起作用,他仍然沒有增加滾動範圍。我設定contentSize self.collectionView.contentSize = CGSizeMake(0, self.view.frame.size.height + deltaY)。我在模擬器5s Xcode7.1上運行應用程序。 contentSize是(0.0,832.0)。爲什麼UIcollectionView無法向上滾動。我設置了UIVCollectionView的contentSize,希望它能夠向上滾動,但它仍然沒有增加滾動範圍

這是方法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() 
     } 


    } 


} 
+1

你沒有自己的內容的大小,所以你可以」不要改變它。最好改變框架大小(你自己做的)。 – Wain

+0

不contentize決定什麼UICollectionView滾動範圍? – rose

+0

是的內容大小定義滾動範圍。而且,收藏視圖有內容大小。但是,它是從佈局計算的,因此您無法明確設置它。 – Wain

回答

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; 
} 
+0

這樣UICollectionView的上面將被阻塞。我想contentView向上滾動,也可以向下滾動。 – rose

+0

不,它不會被阻止,您有權訪問收集視圖的所有內容 – KavyaKavita

+0

它的工作原理,謝謝 – rose