如何获得视图的中心 [英] How to get the centre of the view
本文介绍了如何获得视图的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过使用下面的代码来获得我的视图的中心,所使用的类来自 DTIActivityIndicatorView
,它向视图添加了一个自定义活动指示器.
I'm trying to get the center of my view by using the code below, the class being used is from the DTIActivityIndicatorView
which add a custom activity indicator to the view.
var midX = CGRectGetMidX(self.view.bounds)
var midY = CGRectGetMidY(self.view.bounds)
let topStoriesActivityIndicator: DTIActivityIndicatorView = DTIActivityIndicatorView(frame: CGRect(x: midX, y: midY, width:80.0, height:80.0))
但是上面的代码给了我以下结果,其中活动指示器没有居中.
But the code above is giving me the following result where the activity indicator isn't centered in the middle.
推荐答案
Swift 4 扩展:
extension CGRect {
var center: CGPoint { return CGPoint(x: midX, y: midY) }
}
let someRect = CGRect(x: 10, y: 10, width: 200, height: 40)
let theCenter = someRect.center // {x 110 y 30}
这篇关于如何获得视图的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文