简体   繁体   English

UIScrollView宽度和高度控件

[英]UIScrollView width and height control

Here is how I create and initialise my UIScrollView : 这是我创建和初始化UIScrollView

class MyViewController: UIViewController, UIScrollViewDelegate{

    let items = 20, heightPerItem: CGFloat = 40;
    var scrollView: UIScrollView!
    var contentView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        scrollView = UIScrollView(frame: view.bounds)
        contentView = UIView(frame: view.bounds)

        scrollView.backgroundColor = UIColor.blackColor()
        scrollView.autoresizingMask = UIViewAutoresizing.FlexibleHeight

        var yStart: CGFloat = 0 - heightPerItem
        for i in 0 ..< items {
            yStart += heightPerItem
            let label = UILabel(frame: CGRect(x: 0, y: yStart, width: self.view.bounds.size.width, height: heightPerItem - 1))
            label.text = "\(i) testing..."
            label.backgroundColor = UIColor.brownColor()
            scrollView.addSubview(label)
        }

        view.addSubview(scrollView)
    }

    override func viewDidLayoutSubviews() {
        scrollView.addSubview(contentView)
        scrollView.contentSize.height = heightPerItem * CGFloat(items)
    }
}

And here is the output: 这是输出:

在此处输入图片说明

It could be ok if I never tried orientation change: 如果我从未尝试过更改方向可能没问题:

在此处输入图片说明

So, please look at the following two points: 因此,请注意以下两点:

  1. Is there any other way the scrollView gets height automatically (at least I don't want to do scrollView.contentSize.height = heightPerItem * CGFloat(items) )? 还有其他方法可以使scrollView自动获取高度(至少我不想做scrollView.contentSize.height = heightPerItem * CGFloat(items) )?
  2. How to re-write the code so that the width of the scrollView gets changed just with orientation change? 如何重新编写代码,使scrollView的宽度随方向变化而变化?

I made small changes in your code and placed 我对您的代码做了一些小的改动,然后放置

scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleHeight, .FlexibleWidth]

and

let label = UILabel(frame: CGRect(x: 0, y: yStart, width: self.view.bounds.size.height, height: heightPerItem - 1))

it solved the issue of width in landscape. 它解决了景观的宽度问题。

If you want to control size changes when orientation changes, you can override func viewWillTransitionToSize, for example: 如果要在方向更改时控制大小更改,则可以覆盖func viewWillTransitionToSize,例如:

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    if UIDevice.currentDevice().orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }


}

In addition, for part 1. AFAIK, UIScrollView does not have property that will take maximum of y coordinate, but we can make a small code like this: 另外,对于第1部分。AFAIK,UIScrollView不具有将采用最大y坐标的属性,但是我们可以制作一个像这样的小代码:

    func rectMax() -> CGRect {
       var cR = CGRectZero
       for sv in scrollView.subviews {
           cR = CGRectUnion(cR, sv.frame)
       }

    return cR
}

and than you can use: 然后您可以使用:

        scrollView.contentSize = rectMax().size

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

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