简体   繁体   English

UIScrollView 禁用顶部反弹

[英]UIScrollView disable top bounce

I have a ViewController that contains an UIScrollView and I'm trying to disable the bounce effect only on the top of the view.我有一个包含 UIScrollView 的 ViewController,我试图仅在视图顶部禁用反弹效果。

I tried to add this answer in my ViewController class, which is saying to do this :我试图在我的 ViewController 类中添加这个答案,也就是说要这样做:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    //disable bounce only at the top of the screen
    scrollView.bounces = scrollView.contentOffset.y > 100
}

But I don't really understand what to do with this.但我真的不明白该怎么做。 I also tried to implement the UIScrollViewDelegate.我还尝试实现 UIScrollViewDelegate。

Nothing I tried worked.我试过的都没有用。

Here is my StoryBoard and ViewController这是我的 StoryBoard 和 ViewController

What do I need to do in order to make it works ?我需要做什么才能使其正常工作?

My setup :我的设置:

  • Swift version : 5.1.3斯威夫特版本:5.1.3
  • Xcode version : 11.3.1 Xcode 版本:11.3.1
  • Target iOS version : 13.2目标iOS版本:13.2
  • MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports) MacOS Catalina 10.15.2 (19C57) MacBook Pro(13 英寸,2016 年,四个 Thunderbolt 3 端口)MacOS Catalina 10.15.2 (19C57)

连接 scrollView 插座,然后尝试在viewDidLoad()函数中添加scrollView.delegate = self或添加scrollView.bounces = scrollView.contentOffset.y > 100

If you want to disable bounces of the scrollview then you can take outlet of your scrollview and can set bounces property to false in viewdidload like below,如果您想禁用scrollview的反弹,那么您可以使用滚动视图的出口,并可以在scrollview viewdidload反弹属性设置为 false,如下所示,

yourScrollView.bounces = false

or you can uncheck bounces from the storyboard also!或者您也可以取消选中情节提要中的反弹!

try this one试试这个

func scrollViewDidScroll(scrollView: UIScrollView) {
      if scrollView.contentOffset.y < 0 {
          scrollView.contentOffset.y = 0
      }
  }

Okay here is the answer to my problem :好的,这是我的问题的答案:

  • In the viewController, add the ScrollView outlet在 viewController 中,添加 ScrollView 插座
  • In the viewDidLoad() function, add this myScrollView.delegate = selfviewDidLoad()函数中,添加这个myScrollView.delegate = self
  • Make your viewController implement UIScrollViewDelegate : class MyViewController: UIViewController, UIScrollViewDelegate { ...让你的 viewController 实现 UIScrollViewDelegate : class MyViewController: UIViewController, UIScrollViewDelegate { ...
  • Then add the following function :然后添加以下函数:

    func scrollViewDidScroll(scrollView: UIScrollView) { myScrollView.bounces = myScrollView.contentOffset.y > 100 } func scrollViewDidScroll(scrollView: UIScrollView) { myScrollView.bounces = myScrollView.contentOffset.y > 100 }

Go to Attributes Inspector.转到属性检查器。 uncheck "Bounce On Scrolll"取消选中“滚动反弹”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM