我正在使用 Daniel Cohen Gindi 的开源图表
:https://github.com/danielgindi/Charts
我的屏幕上有一个饼图,其图表如下:
//set pie chart data
graphView.data = PieChartData(dataSets: iPieChartDataSet)
此外,屏幕上有一个按钮,显示弹出窗口 similarly to the accepted answer of this question .
“我想要什么”
当用户按下屏幕上的 UIButton
时,我希望显示一个弹出窗口。
“什么有效”
当我按下 UIButton
时,弹出窗口将按预期创建并呈现。此外,我可以创建弹出窗口,关闭它,然后重新创建它。
“什么不起作用”
如果屏幕上存在图表,则不会出现弹出窗口。我尝试在 UIButton
中使用 print
语句,但看起来 IBAction
从未被触发。
其他信息
其他 UI 操作有效。我可以切换到其他选项卡,也可以旋转图表。如果屏幕上存在图形,UIButton
似乎会以某种方式被关闭。
相关代码: 如何创建弹出窗口:
@IBAction func informationButtonTapped(_ sender: UIButton) {
var popoverContent = (self.storyboard?.instantiateViewController(withIdentifier: segueIdentifiers.informationPopover))! as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSize(width: 500,height: 600)
popover?.delegate = self
popover?.sourceView = self.view
popover?.sourceRect = CGRect(x: 0, y: 0, width: 100, height: 100)
self.present(nav, animated: true, completion: nil)
}
如何创建图表(尽可能缩短):
private func setChart(dataPoints: [String?], values: [Double]) {
var dataEntries: [PieChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = PieChartDataEntry(value: values[i], label: dataPoints[i])
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "")
var iPieChartDataSet: [IChartDataSet] = []
iPieChartDataSet.append(pieChartDataSet)
graphView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
graphView.layer.borderWidth = 0.1
//set pie chart data
graphView.data = PieChartData(dataSets: iPieChartDataSet)
}
最佳答案
对于我的应用程序,我以编程方式创建了按钮,到目前为止我还没有遇到任何问题。这是我的按钮代码,看看它是否适合您。
var updateButton : UIButton = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
setChart()
let buttonHeight = 60.0
let buttonWidth = 280.0
let xspacing = 45.0
let fontSize:CGFloat = 30
let buttonColor = UIColor.black
let textColor = UIColor.blue
updateButton.frame = CGRect(x: xspacing, y: 400, width: buttonWidth, height: buttonHeight)
updateButton.backgroundColor = buttonColor
updateButton.clipsToBounds = true
updateButton.setTitle("Update", for: .normal)
updateButton.titleLabel!.font = UIFont(name: "ScienceFair", size: fontSize)
updateButton.titleLabel!.textColor = UIColor.black
updateButton.addTarget(self, action: #selector(updateFunc), for: .touchUpInside)
view.addSubview(updateButton)
}
func updateFunc() {
UIView.animate(withDuration: 0.1, animations: { self.updateButton.transform = CGAffineTransform(scaleX: 0.9, y: 0.9) }, completion: { (finish: Bool) in UIView.animate(withDuration: 0.1, animations: { self.updateButton.transform = CGAffineTransform.identity }) })
//Present Popover
}
忽略注释的 #selector 的 stackoverflow 格式。这不是评论。
关于ios - 在 "Charts by Daniel Cohen Gindi"的图表绘制自身之后,屏幕上的 UIButton 不再可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41094311/