【问题标题】:Update height of UIScrollView更新 UIScrollView 的高度
【发布时间】:2019-03-03 02:02:06
【问题描述】:

我在 xib 文件中有以下设置

ScrollView (UIScrollView)
    - ContentView (UIView)
        - ImageSlider (FSPagerView)
        - ClinicName (UILabel)
        - ClinicSlogan (UILabel)
        - LineSeparatorView (UIView)
        - ClinicProfile (UITextView)
        - SectionView (Custom UIView)
        - Doctors View (UICollectionView) - Horizontal Scrolling

在 IB 中通过以下方式设置约束

// ScrollView Constraints
ScrollView.Top = NavigationView.Bottom
ScrollView.Leading = SafeArea.Leading
SafeArea.Trailing = ScrollView.Trailing
SafeArea.Bottom = ScrollView.Bottom

// ContentView Constraints
ContentView.Top = SuperView.Top
ContentView.Leading = SuperView.Leading
SuperView.Trailing = ContentView.Trailing
SuperView.Bottom = ContentView.Bottom
ContentView.Height = SuperView.Height (Priority@250)
ContentView.Width = SuperView.Width

ContentView 内部子视图的所有约束都与 SuperView (ContentView) 相关设置

viewDidLoad() 中,我正在更新 ClinicSlogan 和 ClinicProfile 的文本值并更新高度常数。哪个有效,但是当我更新滚动视图的高度时它不起作用,这是我在下面调用的代码 viewDidLoad()

self.clinicSloganLabelHeightConstraint.constant = estimatedFrameFor(
    text: self.clinicSloganLabel.text!,
    font: self.clinicSloganLabel.font!,
    width: self.clinicSloganLabel.frame.width
).height
self.clinicProfileTextViewHeightConstraint.constant = self.clinicProfileTextView.sizeThatFits(
    CGSize(width: self.clinicProfileTextView.frame.width, height: .infinity)
).height
self.scrollView.contentSize.height = 2000

我尝试将代码放在 viewDidLayoutSubviews() 中,但问题是,我在 ScrollView() 中使用 UICollectionView(),它会在调用 viewDidLayoutSubviews 方法时停止滚动。

你能帮助在 UIViewController 子类中更新滚动视图的高度吗?

谢谢

【问题讨论】:

    标签: uiscrollview interface-builder swift4 xib


    【解决方案1】:

    好的,我在 IB 中使用 Autolayout 解决了这个问题,我所做的是,我在 Content View 中添加“Bottom Space To Container”约束的最后一个视图,现在所有内容都在自动调整。

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多