2015-11-03 74 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