UIscrollView 不工作
UIscrollView not work
我将此代码下载到图表中:
https://github.com/tmdvs/CoreGraphicsGraph
我正在尝试以编程方式插入水平和垂直滚动视图。
我尝试这样做,但它不起作用:
override func viewDidLoad() {
super.viewDidLoad()
parser.delegate = self
parser.parse {
let myData = [
["label" : "2000", "value" : NSNumber(int:15)],
["label" : "2001", "value" : NSNumber(int:30)],
["label" : "2002", "value" : NSNumber(int:7)],
["label" : "2003", "value" : NSNumber(int:60)],
["label" : "2004", "value" : NSNumber(int:30)],
["label" : "2005", "value" : NSNumber(int:15)],
["label" : "2006", "value" : NSNumber(int:45)],
["label" : "2007", "value" : NSNumber(int:45)],
["label" : "2008", "value" : NSNumber(int:50)],
["label" : "2009", "value" : NSNumber(int:60)],
["label" : "2010", "value" : NSNumber(int:70)],
["label" : "2011", "value" : NSNumber(int:80)],
["label" : "2012", "value" : NSNumber(int:90)],
["label" : "2013", "value" : NSNumber(int:100)],
["label" : "2014", "value" : NSNumber(int:200)],
["label" : "2015", "value" : NSNumber(int:300)],
["label" : "2016", "value" : NSNumber(int:400)],
["label" : "2017", "value" : NSNumber(int:500)],
["label" : "2018", "value" : NSNumber(int:600)],
["label" : "2019", "value" : NSNumber(int:700)],
["label" : "2020", "value" : NSNumber(int:800)],
["label" : "2021", "value" : NSNumber(int:900)],
] as NSArray
// ScrollView
let scrollView : UIScrollView = UIScrollView(frame : CGRect(x:1000, y:(1000), width: (1000), height: (1000) ))
scrollView.scrollEnabled = true
scrollView.contentSize = CGSize(width:1234, height: 5678)
// Textview
let textView : UITextView = UITextView(frame : CGRect(x:400, y:(500), width: (100), height: (100) ))
textView.text = "SeriesKey"
// graph
let graph = GraphView(frame: CGRectMake(50, 100, 900, 300), data: myData)
// Add sub view
self.view.addSubview(textView)
self.view.addSubview(graph)
self.view.addSubview(scrollView)
}
}
有什么建议吗?谢谢。
您的示例无效,因为您的 UIScrollView
没有内容。要使其正常工作,请将要显示的内容添加到滚动视图中,例如:
let scrollView = UIScrollView(frame: CGRectMake(0, 0, 100, 100))
let contentView = UIView(frame: CGRectMake(0.0, 0.0, 200, 200))
contentView.backgroundColor = UIColor.blackColor()
contentView.layer.borderColor = UIColor.redColor().CGColor
contentView.layer.borderWidth = 10.0
// This is important!
scrollView.addSubview(contentView)
scrollView.contentSize = view.bounds.size
scrollView.scrollEnabled = true
addSubview(scrollView)
这将为您提供一个带有漂亮红色边框的可滚动黑色子视图。另外,请看看Apple提供的Scroll View Guide。
我将此代码下载到图表中:
https://github.com/tmdvs/CoreGraphicsGraph
我正在尝试以编程方式插入水平和垂直滚动视图。
我尝试这样做,但它不起作用:
override func viewDidLoad() {
super.viewDidLoad()
parser.delegate = self
parser.parse {
let myData = [
["label" : "2000", "value" : NSNumber(int:15)],
["label" : "2001", "value" : NSNumber(int:30)],
["label" : "2002", "value" : NSNumber(int:7)],
["label" : "2003", "value" : NSNumber(int:60)],
["label" : "2004", "value" : NSNumber(int:30)],
["label" : "2005", "value" : NSNumber(int:15)],
["label" : "2006", "value" : NSNumber(int:45)],
["label" : "2007", "value" : NSNumber(int:45)],
["label" : "2008", "value" : NSNumber(int:50)],
["label" : "2009", "value" : NSNumber(int:60)],
["label" : "2010", "value" : NSNumber(int:70)],
["label" : "2011", "value" : NSNumber(int:80)],
["label" : "2012", "value" : NSNumber(int:90)],
["label" : "2013", "value" : NSNumber(int:100)],
["label" : "2014", "value" : NSNumber(int:200)],
["label" : "2015", "value" : NSNumber(int:300)],
["label" : "2016", "value" : NSNumber(int:400)],
["label" : "2017", "value" : NSNumber(int:500)],
["label" : "2018", "value" : NSNumber(int:600)],
["label" : "2019", "value" : NSNumber(int:700)],
["label" : "2020", "value" : NSNumber(int:800)],
["label" : "2021", "value" : NSNumber(int:900)],
] as NSArray
// ScrollView
let scrollView : UIScrollView = UIScrollView(frame : CGRect(x:1000, y:(1000), width: (1000), height: (1000) ))
scrollView.scrollEnabled = true
scrollView.contentSize = CGSize(width:1234, height: 5678)
// Textview
let textView : UITextView = UITextView(frame : CGRect(x:400, y:(500), width: (100), height: (100) ))
textView.text = "SeriesKey"
// graph
let graph = GraphView(frame: CGRectMake(50, 100, 900, 300), data: myData)
// Add sub view
self.view.addSubview(textView)
self.view.addSubview(graph)
self.view.addSubview(scrollView)
}
}
有什么建议吗?谢谢。
您的示例无效,因为您的 UIScrollView
没有内容。要使其正常工作,请将要显示的内容添加到滚动视图中,例如:
let scrollView = UIScrollView(frame: CGRectMake(0, 0, 100, 100))
let contentView = UIView(frame: CGRectMake(0.0, 0.0, 200, 200))
contentView.backgroundColor = UIColor.blackColor()
contentView.layer.borderColor = UIColor.redColor().CGColor
contentView.layer.borderWidth = 10.0
// This is important!
scrollView.addSubview(contentView)
scrollView.contentSize = view.bounds.size
scrollView.scrollEnabled = true
addSubview(scrollView)
这将为您提供一个带有漂亮红色边框的可滚动黑色子视图。另外,请看看Apple提供的Scroll View Guide。