垂直的UISCrollView以编程方式

发布于 2025-02-09 01:20:06 字数 1343 浏览 3 评论 0原文

我添加了一些约束,但是它水平滚动,问题在哪里我该如何管理垂直滚动?我试图在单独的内容的宽度和高度上添加一些额外的点,但没有结果可以水平滚动。

  1. 我刚刚意识到第二个问题,在添加scrollview之后,Uiview内部的最喜欢的按钮就开始工作,但是,在scrollview内部的contentView子视图中添加了按钮。 提前致谢!
private func setupScrollView() {
      self.view.addSubview(scrollView)
      scrollView.addSubview(contentView)
      scrollView.translatesAutoresizingMaskIntoConstraints = false
      contentView.translatesAutoresizingMaskIntoConstraints = false
      
      scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height + 1000)
      
      NSLayoutConstraint.activate([
          scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
          scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
          scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
          scrollView.leadingAnchor.constraint(equalTo:self.view.leadingAnchor)
      ])
          NSLayoutConstraint.activate([
              contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
              contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
              contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
              contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
      ])

I added some constraints but it scrolls horizontally, Where is the problem how can I manage it to scroll vertically? I tried to add some extra points to the width and height of contentSize separately but no results it keeps scrolling horizontally.

  1. And I just realized second problem my fav button inside UIView started not working after I added ScrollView, however, button added to the UIView that subview of ContentView which inside of ScrollView.
    Thanks in Advance!
private func setupScrollView() {
      self.view.addSubview(scrollView)
      scrollView.addSubview(contentView)
      scrollView.translatesAutoresizingMaskIntoConstraints = false
      contentView.translatesAutoresizingMaskIntoConstraints = false
      
      scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height + 1000)
      
      NSLayoutConstraint.activate([
          scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
          scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
          scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
          scrollView.leadingAnchor.constraint(equalTo:self.view.leadingAnchor)
      ])
          NSLayoutConstraint.activate([
              contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
              contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
              contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
              contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
      ])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天冷不及心凉 2025-02-16 01:20:07

您需要在content> uiscrollview高度中设置view> viewWilllayOutsUbviews中:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.scrollView.contentSize.height = self.view.frame.height + 1000
}

并删除此行:<代码> scrollview.contentsize = cgsize(width:self.view.frame.width,高度:self.view.frame.frame.height + 1000)

You need to set the height of the content for the UIScrollView in the viewWillLayoutSubviews:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.scrollView.contentSize.height = self.view.frame.height + 1000
}

And remove this line: scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height + 1000)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文