繁体   English   中英

使SubView适应View Swift

[英]Fitting subView to View Swift

如何将子视图适合在故事板上创建的视图的“适当尺寸”?

例如

self.popUpView.addSubview(chart)

正在展示我想在故事板上显示的视图的显示。

在此处输入图片说明

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

您尚未真正发布解决方案popUpView.frame的问题,但我注意到的直接一件事是,将Chartframe设置为popUpView.frame ,而应将其设置为popUpView.bounds 这将确保Chart的原点是popUpView的坐标系内的popUpView的原点。

因此,为了显示该示例,请将您的初始化代码更改为:

let chart = Chart(frame: self.popUpView.bounds)

如果要创建一个使图表与popUpView相同大小的popUpView ,但要使其距底部popUpView (例如),您可以说:

let chart = Chart(frame: CGRect(x: 0, y: 0, width: self.popUpView.bounds.size.width, height: self.popUpView.bounds.size.height - 20))

也许您希望它是popUpView高度的popUpView

let chart = Chart(frame: CGRect(x: 0, y: 0, width: self.popUpView.bounds.size.width, height: self.popUpView.bounds.size.height / 2))

等等。

暂无
暂无

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

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