다른 장치 방향에서 UICollectionViewCell 크기 변경
UICollectionView
와 함께 사용하고 있습니다 UICollectionViewFlowLayout
.
나는 각 셀의 크기를
collectionView:layout:sizeForItemAtIndexPath:
세로에서 가로로 전환 할 때 셀 사이에 패딩 공간을 남기지 않고 CollectionView의 크기에 완전히 맞도록 각 셀의 크기를 조정하고 싶습니다.
질문 :
1) 회전 이벤트 후 셀 크기를 변경하는 방법은 무엇입니까?
2) 그리고 더 나은 방법은 셀이 항상 화면 전체 크기에 맞는 레이아웃을 만드는 방법입니까?
1) 여러 레이아웃 개체를 유지하고 교체 할 수 있지만 더 간단한 방법이 있습니다. 다음을 UICollectionViewController
하위 클래스에 추가하고 필요에 따라 크기를 조정하십시오.
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Adjust cell size for orientation
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
return CGSizeMake(170.f, 170.f);
}
return CGSizeMake(192.f, 192.f);
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.collectionView performBatchUpdates:nil completion:nil];
}
호출 -performBatchUpdates:completion:
하면 레이아웃이 무효화되고 애니메이션으로 셀 크기가 조정됩니다 (수행 할 추가 조정이없는 경우 두 블록 매개 변수에 nil을 전달할 수 있습니다).
2)에서 항목 크기를 하드 코딩하는 대신 -collectionView:layout:sizeForItemAtIndexPath
collectionView 경계의 높이 또는 너비를 화면에 맞출 셀 수로 나누십시오. collectionView가 가로로 스크롤되는 경우 높이를 사용하고 세로로 스크롤하는 경우 너비를 사용하십시오.
에 의해 추가 애니메이션을 가져 오지 않으려면을 performBatchUpdates:completion:
사용 invalidateLayout
하여 collectionViewLayout을 강제로 다시로드하십시오.
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self.collectionView.collectionViewLayout invalidateLayout];
}
두 개의 UICollectionViewFlowLayout을 사용하여 해결했습니다. 하나는 세로 용이고 다른 하나는 가로 용입니다. -viewDidLoad에서 내 collectionView에 동적으로 할당합니다.
self.portraitLayout = [[UICollectionViewFlowLayout alloc] init];
self.landscapeLayout = [[UICollectionViewFlowLayout alloc] init];
UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientationOnLunch)) {
[self.menuCollectionView setCollectionViewLayout:self.portraitLayout];
} else {
[self.menuCollectionView setCollectionViewLayout:self.landscapeLayout];
}
그런 다음 단순히 collectionViewFlowLayoutDelgate 메서드를 다음과 같이 수정했습니다.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGSize returnSize = CGSizeZero;
if (collectionViewLayout == self.portraitLayout) {
returnSize = CGSizeMake(230.0, 120.0);
} else {
returnSize = CGSizeMake(315.0, 120.0);
}
return returnSize;
}
Last I switch from a layout to one other on rotation
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if (UIInterfaceOrientationIsPortrait(fromInterfaceOrientation)) {
[self.menuCollectionView setCollectionViewLayout:self.landscapeLayout animated:YES];
} else {
[self.menuCollectionView setCollectionViewLayout:self.portraitLayout animated:YES];
}
}
There's a simple way to set collection view cell size:
UICollectionViewFlowLayout *layout = (id) self.collectionView.collectionViewLayout;
layout.itemSize = CGSizeMake(cellSize, cellSize);
Not sure if it's animatable though.
Swift : Collection View Cell that is n% of screen height
I needed was a cell that was a certain percentage of views height, and would resize with device rotation.
With help from above, here is the solution
override func viewDidLoad() {
super.viewDidLoad()
automaticallyAdjustsScrollViewInsets = false
// if inside nav controller set to true
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
collectionViewLayout.invalidateLayout()
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 100, height: collectionView.frame.height * 0.9)
}
If you are using autolayout
.You just need to invalidate layout
in your view controller when rotating and adjust offset
in your flow layout subclass.
So, In your view controller -
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
self.galleryCollectionView.collectionViewLayout.invalidateLayout()
}
And in your FlowLayout Subclass
override func prepareLayout() {
if self.collectionView!.indexPathsForVisibleItems().count > 0{
var currentIndex : CGFloat!
currentIndex = CGFloat((self.collectionView!.indexPathsForVisibleItems()[0] as! NSIndexPath).row)
if let currentVal = currentIndex{
self.collectionView!.contentOffset = CGPointMake(currentIndex * self.collectionView!.frame.width, self.collectionView!.contentOffset.y);
}
}
}
'IT박스' 카테고리의 다른 글
angularjs 필터에 인수 전달 (0) | 2020.08.21 |
---|---|
입력에 대해 프로그래밍 방식으로 onchange 이벤트를 강제하는 방법은 무엇입니까? (0) | 2020.08.20 |
notifyDatasetChanged () 후 RecyclerView 깜박임 (0) | 2020.08.20 |
폴더에 파일이 있는지 확인하는 방법은 무엇입니까? (0) | 2020.08.20 |
신속한 사운드 생성 및 재생 (0) | 2020.08.20 |