在iOS 13的UIScrollView中设置UIRefreshControl

时间:2019-10-03 10:11:02

标签: ios swift ios13

我在滚动视图上添加UIRefreshControl,在iOS 13以下可以正常工作,但是当我在iOS 13上运行相同的应用程序时,它崩溃并显示以下日志。

  

在ScrollView上添加UIRefreshControl

let refresh = UIRefreshControl()

    //Scroll View main
    @IBOutlet weak var scrollViewMain: UIScrollView!



    override func viewDidLoad() {
        super.viewDidLoad()
        pullToRefresh()

    }

    func pullToRefresh(){
        refresh.addTarget(self, action: #selector(handleTopRefresh(_:)), for: .valueChanged )
        refresh.tintColor = UIColor.themeAppColor
        self.scrollViewMain.refreshControl = refresh
      }
    @objc func handleTopRefresh(_ sender:UIRefreshControl){
        getOutletRatings()
    }
  

崩溃日志

MyApp Test[33324:98294] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<MyApp_Test.OutletRatingVC: 0x7faa2e806a00> returned nil from -traitCollection, which is not allowed.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23b98bde __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff503b5b20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23b98958 +[NSException raise:format:arguments:] + 88
    3   Foundation                          0x00007fff255eb7be -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 166
    4   UIKitCore                           0x00007fff46dd08bd _UIGetCurrentFallbackTraitCollection + 962
    5   UIKitCore                           0x00007fff476029fa UIViewCommonInitWithFrame + 617
    6   UIKitCore                           0x00007fff47602753 -[UIView initWithFrame:] + 98
    7   UIKitCore                           0x00007fff46b910bb -[_UIRefreshControlModernContentView initWithFrame:] + 70
    8   UIKitCore                           0x00007fff46b6343d -[UIRefreshControl _contentView] + 150
    9   UIKitCore                           0x00007fff46b631ab -[UIRefreshControl sizeToFit] + 52
    10  UIKitCore                           0x00007fff46b632b1 -[UIRefreshControl didMoveToSuperview] + 178
    11  UIKitCore                           0x00007fff476207de __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 1010
    12  UIKitCore                           0x00007fff47620373 -[UIView(Hierarchy) _postMovedFromSuperview:] + 753
    13  UIKitCore                           0x00007fff4762fb45 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1718
    14  UIKitCore                           0x00007fff475c396d -[UIScrollView _addContentSubview:atBack:] + 564
    15  UIKitCore                           0x00007fff475bf3df -[UIScrollView _setRefreshControl:] + 248
    16  UIKitCore                           0x00007fff475bf265 -[UIScrollView setRefreshControl:] + 67
    17  MyApp Test                      0x000000010d5cd02e $s14MyApp_Test14OutletRatingVCC13pullToRefreshyyF + 270
    18  MyApp Test                      0x000000010d5cce7e $s14MyApp_Test14OutletRatingVCC11viewDidLoadyyF + 190
    19  MyApp Test                      0x000000010d5ccf0b $s14MyApp_Test14OutletRatingVCC11viewDidLoadyyFTo + 43
    20  UIKitCore                           0x00007fff46af87c2 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 83
    21  UIKitCore                           0x00007fff46afd6d3 -[UIViewController loadViewIfRequired] + 1084
    22  UIKitCore                           0x00007fff46afdaf0 -[UIViewController view] + 27
    23  UIKitCore                           0x00007fff46a81dbf -[UIPageViewController _setViewControllers:withScrollInDirection:animated:completion:] + 538
    24  UIKitCore                           0x00007fff46a81fd3 -[UIPageViewController setViewControllers:direction:animated:completion:] + 263
    25  CarbonKit                           0x000000010e3afb01 -[CarbonTabSwipeNavigation loadFirstViewController] + 705
    26  CarbonKit                           0x000000010e3aaba1 -[CarbonTabSwipeNavigation initWithItems:toolBar:delegate:] + 289
    27  MyApp Test                      0x000000010d32960a $sSo24CarbonTabSwipeNavigationC5items7toolBar8delegateABSayypGSg_So9UIToolbarCyptcfcTO + 250
    28  MyApp Test                      0x000000010d32828a $sSo24CarbonTabSwipeNavigationC5items7toolBar8delegateABSayypGSg_So9UIToolbarCyptcfC + 58
    29  MyApp Test                      0x000000010d327159 $s14MyApp_Test15RatingMainTabVCC11viewDidLoadyyF + 409
    30  MyApp Test                      0x000000010d3282cb $s14MyApp_Test15RatingMainTabVCC11viewDidLoadyyFTo + 43
    31  UIKitCore                           0x00007fff46af87c2 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 83
    32  UIKitCore                           0x00007fff46afd6d3 -[UIViewController loadViewIfRequired] + 1084
    33  UIKitCore                           0x00007fff46a6208c -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 160
    34  UIKitCore                           0x00007fff46a6238c -[UINavigationController _startTransition:fromViewController:toViewController:] + 140
    35  UIKitCore                           0x00007fff46a63256 -[UINavigationController _startDeferredTransitionIfNeeded:] + 868
    36  UIKitCore                           0x00007fff46a645c1 -[UINavigationController __viewWillLayoutSubviews] + 150
    37  UIKitCore                           0x00007fff46a45ffb -[UILayoutContainerView layoutSubviews] + 217
    38  UIKitCore                           0x00007fff47636722 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    39  QuartzCore                          0x00007fff2b030ef9 -[CALayer layoutSublayers] + 255
    40  QuartzCore                          0x00007fff2b0358ff _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
    41  QuartzCore                          0x00007fff2b041fe4 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
    42  QuartzCore                          0x00007fff2af8a4a8 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
    43  QuartzCore                          0x00007fff2afbfab3 _ZN2CA11Transaction6commitEv + 643
    44  UIKitCore                           0x00007fff4715c27c _UIApplicationFlushRunLoopCATransactionIfTooLate + 104
    45  UIKitCore                           0x00007fff471fd878 __handleEventQueueInternal + 6902
    46  CoreFoundation                      0x00007fff23afbac1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    47  CoreFoundation                      0x00007fff23afb9ec __CFRunLoopDoSource0 + 76
    48  CoreFoundation                      0x00007fff23afb1c4 __CFRunLoopDoSources0 + 180
    49  CoreFoundation                      0x00007fff23af5ecf __CFRunLoopRun + 1263
    50  CoreFoundation                      0x00007fff23af56b6 CFRunLoopRunSpecific + 438
    51  GraphicsServices                    0x00007fff3815cbb0 GSEventRunModal + 65
    52  UIKitCore                           0x00007fff47162a67 UIApplicationMain + 1621
    53  MyApp Test                      0x000000010d4769cb main + 75
    54  libdyld.dylib                       0x00007fff5123bcf5 start + 1
    55  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1 个答案:

答案 0 :(得分:0)

我假设您在viewDidLoad中没有特征集。将pullToRefresh()移至viewWillAppear,然后更改pullToRefresh函数

func pullToRefresh() {
    if scrollViewMain.refreshControl == nil {
        refresh.addTarget(self, action: #selector(handleTopRefresh(_:)), for: .valueChanged )
        refresh.tintColor = UIColor.blue
        self.scrollViewMain.refreshControl = refresh
    }
}