如何使 subview 适合我在 Storyboard中创建的 View 的正确尺寸?
例如
self.popUpView.addSubview(chart)
正在展示我想跨越 Storyboard中的 View 的展示。
class PopVC: UIViewController {
@IBOutlet weak var popUpView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let chart = Chart(frame: self.popUpView.frame)
let data = [(x: 0.0, y: 0), (x: 3, y: 2.5), (x: 4, y: 2), (x: 5, y: 2.3), (x: 7, y: 3), (x: 8, y: 2.2), (x: 9, y: 2.5)]
let series = ChartSeries(data: data)
series.area = true
chart.xLabels = [0, 3, 6, 9, 12, 15, 18, 21, 24]
chart.xLabelsFormatter = { String(Int(round($1))) + "h" }
chart.add(series)
//Circle Edge on barChartView
popUpView.layer.cornerRadius = 10
popUpView.layer.masksToBounds = true
self.popUpView.addSubview(chart)
}
@IBAction func closePopUp(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}//END CLASS
最佳答案
您还没有真正发布您的解决方案有什么问题,但我立即注意到您正在将 Chart
的 frame
设置为popUpView.frame
,什么时候应该设置为popUpView.bounds
。这将确保 Chart
的原点是 popUpView
坐标系中 popUpView
的原点。
因此,为了展示示例,请将您的初始化代码更改为:
let chart = Chart(frame: self.popUpView.bounds)
如果您想创建一个矩形,使图表与 popUpView
大小相同,但距底部 20pt(例如),您可以说:
let chart = Chart(frame: CGRect(x: 0, y: 0, width: self.popUpView.bounds.size.width, height: self.popUpView.bounds.size.height - 20))
也许你希望它是 popUpView
高度的一半:
let chart = Chart(frame: CGRect(x: 0, y: 0, width: self.popUpView.bounds.size.width, height: self.popUpView.bounds.size.height / 2))
等等
关于ios - 将 subView 适配到 View Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798974/