Swift中的iOS应用程序上的ScrollView

时间:2015-10-05 08:57:46

标签: ios swift scroll uiscrollview

我有一个ViewController,我的View太大了,所以我试图实现一个简单的ScrollView,但我不知道该怎么做。我试图做Embed in >> ScrollView,但它不起作用。

2 个答案:

答案 0 :(得分:0)

在VC的xib或storyboard文件中添加Scrollview。单独创建视图并将其作为子视图添加到scrollview中。或者您只需将其拖动到滚动视图中即可。将scrollview的内容大小设置为等于该视图的大小。如果您仍需要帮助,请使用本教程。 http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift

答案 1 :(得分:0)

如果您需要宽屏,那么这里举例说明如何制作比平时宽三倍的示例。

@IBOutlet weak var scrollView: UIScrollView!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let view1: View1 = View1(nibName: "View1", bundle: nil)

    addChildViewController(view1)
    scrollView.addSubview(view1.view)
    view1.didMoveToParentViewController(self)

    let view2: View2 = View2(nibName: "View2", bundle: nil)

    addChildViewController(view2)
    scrollView.addSubview(view2.view)
    view2.didMoveToParentViewController(self)

    var view2Frame: CGRect = view2.view.frame
    view2Frame.origin.x = view.frame.width
    view2.view.frame = view2Frame


    let view3: View3 = View3(nibName: "View3", bundle: nil)

    addChildViewController(view3)
    scrollView.addSubview(view3.view)
    view3.didMoveToParentViewController(self)

    var view3Frame: CGRect = view3.view.frame
    view3Frame.origin.x = view.frame.width * 2
    view3.view.frame = view3Frame

    self.scrollView.contentSize.width = view.frame.width * 3
  }

所以基本上你创建了scrollView并在这里添加了三个视图,每个视图都有它的新原点。您可以在故事板中启用或禁用分页。就是这样!