Swift:添加UIScroll视图以编程方式不在视图中滚动

时间:2017-01-10 06:48:13

标签: ios swift uiscrollview

我正在尝试以编程方式实现UIScrollView

这背后的主要原因是我首先在我的ViewController中添加了视图,最后我发现忘记在我的ViewController中添加滚动视图。现在我想添加它programiticaly但不会在视图中添加。我到处搜索并试过但还没有帮助。

   @IBOutlet weak var UIViewHeader: UIView!
@IBOutlet weak var btnRegisteredCourses: UIButton!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var tableView: UITableView!

var scrollView: UIScrollView!

   override func viewDidLoad() {
    super.viewDidLoad()
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.contentSize = view.bounds.size
    scrollView.scrollEnabled = true
    scrollView.addSubview(UIViewHeader)
    scrollView.addSubview(lblName)
    scrollView.addSubview(tableView)
    scrollView.addSubview(btnRegisteredCourses)
    view.addSubview(scrollView)
  }

5 个答案:

答案 0 :(得分:3)

override func viewDidLayoutSubviews()
{
    scrollView.delegate = self
    scrollView.contentSize = CGSize(width:self.view.frame.size.width, height: 1000)

}

答案 1 :(得分:1)

增加滚动视图内容大小并重试。

你必须从超级视图中删除UIViewHeader,lblName,tableView,btnRegisteredCourses,然后添加到滚动视图。

答案 2 :(得分:1)

尝试此内容也会滚动:

 @IBOutle var UIViewHeader: UIView!
@IBOutle var btnRegisteredCourses: UIButton!
@IBOutle var lblName: UILabel!
@IBOutle var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.contentSize = CGSize(width:self.view.frame.size.width, height: 1000)
    scrollView.scrollEnabled = true

UIViewHeader.removeFromSuperview()
lblName.removeFromSuperview()
tableView.removeFromSuperview()
btnRegisteredCourses.removeFromSuperview()
    scrollView.addSubview(UIViewHeader)
    scrollView.addSubview(lblName)
    scrollView.addSubview(tableView)
    scrollView.addSubview(btnRegisteredCourses)
    view.addSubview(scrollView)
  }

答案 3 :(得分:1)

取决于您需要哪种类型的滚动,您必须设置contentSize的{​​{1}}。例如

水平滚动:

scrollView

垂直滚动:

scrollView.contentSize = CGSize(width: UIViewHeader.frame.size.width + lblName.frame.size.width + tableView.frame.size.width + btnRegisteredCourses.frame.size.width , height: scrollView.frame.size.height)

这样做:

scrollView.contentSize = CGSize(width: scrollView.frame.size.frame.size.width , height: UIViewHeader.frame.size.height + lblName.frame.size.height + tableView.frame.size.height + btnRegisteredCourses.frame.size.height)

答案 4 :(得分:0)

    import UIKit
class ViewController: UIViewController {
@IBOutlet weak var UIViewHeader: UIView!
@IBOutlet weak var btnRegisteredCourses: UIButton!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var tableView: UITableView!

var scrollView: UIScrollView!
override func viewDidLoad() {
    super.viewDidLoad()
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.contentSize = CGSize.init(width: self.view.frame.width, height: 1000)       //1000 change to the scollview's size.
    scrollView.addSubview(UIViewHeader)
    scrollView.addSubview(lblName)
    scrollView.addSubview(tableView)
    scrollView.addSubview(btnRegisteredCourses)
   view.addSubview(scrollView)

}
 }

**注意: - 但是上面的代码重置所有约束**