iOS14 UIPageControl和scrollToItemAtIndexPath失效的问题

BUG一:UIPageControl不显示

之前好端端的UIPageControl到了iOS14突然就不显示了表示很郁闷☹️

解决方案

1、按照系统规则适配pageControl size

size = CGSizeMake(_imagesArray.count * 10 * 1.5, 10);
if (@available(iOS 14.0, *)) {
    UIPageControl *pageControl = (UIPageControl *)_pageControl;
    size.width = [pageControl sizeForNumberOfPages:_imagesArray.count].width;
}
_pageControl.frame = xxxx;

2、添加约束适配(或者使用第三方添加约束)

size = CGSizeMake(_imagesArray.count * 10 * 1.5, 10);
self.pageControl.translatesAutoresizingMaskIntoConstraints = NO;
[[self.pageControl.bottomAnchor constraintEqualToAnchor:self.bottomAnchor
                                               constant:-_pageControlBottomOffset] setActive:YES];
[[self.pageControl.heightAnchor constraintEqualToConstant:size.height] setActive:YES];
if (_pageControlAliment == YXCycleScrollViewPageContolAlimentRight) {
  [[self.pageControl.rightAnchor constraintEqualToAnchor:self.rightAnchor
                                                          constant:-_pageControlRightOffset] setActive:YES];
} else{
   [[self.pageControl.centerXAnchor constraintEqualToAnchor:self.centerXAnchor] setActive:YES];
}

BUG二:collectionView scrollToItemAtIndexPath失效

这里失效的原因是对UICollectionView设置了分页属性pagingEnabled导致的,所以方案一我们可以关闭分页效果。

  • 方案二:用下面的方法替代(或者根据Item大小来自己计算contentOffset
UICollectionViewLayoutAttributes *attributes = [_collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0]];
_collectionView.contentOffset = CGPointMake(attributes.frame.origin.x - _itemSpacing, 0);

你可能感兴趣的:(iOS14 UIPageControl和scrollToItemAtIndexPath失效的问题)