简体   繁体   English

更改 UIScrollView 内容大小

[英]Change UIScrollView Content size

I have ViewController with UIScrollView, some UIImages and UIButtons.我有带 UIScrollView 的 ViewController、一些 UIImages 和 UIButtons。 In the Storyboard I set the size of UIScrollView to: width: 320, height: 570在 Storyboard 中,我将 UIScrollView 的大小设置为:宽度:320,高度:570

I want to change Contentsize of my UIScrollView when the ViewController was loaded.我想在加载 ViewController 时更改 UIScrollView 的 Contentsize。

I added side in the viewDidLoad:我在 viewDidLoad 中添加了 side:

        scrlMain.contentSize = CGSize(width: 320,height: 790)

And also I added code in the viewDidLayoutSubviews:我还在 viewDidLayoutSubviews 中添加了代码:

        self.scrlMain.translatesAutoresizingMaskIntoConstraints = true

But my UIScrollView is still the last size = 570. I can't use viewDidAppear because, when I press the button, it need it to change color.但是我的 UIScrollView 仍然是最后一个尺寸 = 570。我不能使用 viewDidAppear,因为当我按下按钮时,它需要它来改变颜色。 When it has changed color, my UIScrollView moves to up.当它改变颜色时,我的 UIScrollView 向上移动。

What did I do wrong?我做错什么了?

All code:所有代码:

    override func viewDidLoad() {
    super.viewDidLoad()
    scrlMain.delegate = self
    scrlMain.contentSize = CGSizeMake(320, 790)
    }
override func viewDidLayoutSubviews() {
    self.scrlMain.translatesAutoresizingMaskIntoConstraints = true
    self.scrlMain.contentSize = CGSize(width: self.scrlMain.frame.width, height: 60.0 + 5 * (self.scrlMain.frame.width / 16.0 * 5.0));
}

there was an error in the this function: 这个函数有一个错误:

override func viewDidLayoutSubviews() {
self.scrlMain.translatesAutoresizingMaskIntoConstraints = true
self.scrlMain.contentSize = CGSize(width: self.scrlMain.frame.width, height: 60.0 + 5 * (self.scrlMain.frame.width / 16.0 * 5.0));
}

I delete this code: 我删除了这段代码:

self.scrlMain.contentSize = CGSize(width: self.scrlMain.frame.width, height: 60.0 + 5 * (self.scrlMain.frame.width / 16.0 * 5.0));

And all was right 一切都是对的

Hey @Dim you can try this- 嘿@Dim你可以尝试这个 -

yourScrollView.contentSize = CGSizeMake(320, 568)

And you can change size 568 to whatever you want. 您可以将尺寸568更改为您想要的任何尺寸。

Try this: 试试这个:

yourScrollView.userInteractionEnabled = YES

and you please maintain the difference between your screen size and your scrollview content size... so that it can be visible properly 并且请保持屏幕大小和滚动视图内容大小之间的差异...以便正确显示它

suppose yourScrollViewSize greatertThan yourScreenSize 假设yourScrollViewSize greatertThan yourScreenSize

override func viewDidLayoutSubviews() {

    DispatchQueue.main.async {
    self.scrollview.translatesAutoresizingMaskIntoConstraints = true
    self.scrollview.contentSize = CGSize(width:0, height:self.scrollview.frame.height*2) // whatever hight you want
    }
}

It is also possible to change the content size from within the page without running ViewDidLoad again.也可以在页面内更改内容大小而无需再次运行 ViewDidLoad。

Just add只需添加

scrollView.layoutIfNeeded()

after you set the new contentSize设置新的 contentSize 后

Example when you change the content size ( viewImages ) within the UIscrollView ( scrollViewImages )在 UIscrollView ( scrollViewImages ) 中更改内容大小 ( viewImages ) 的示例

viewImages.frame.size = CGSize(width: width, height: 128)
viewImages.translatesAutoresizingMaskIntoConstraints = true
viewImages.setNeedsLayout()
scrollViewImages.setNeedsLayout()
scrollViewImages.layoutIfNeeded()

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

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