Here's the Swift 3 version of the accepted answer, as a UIView extension with offset arguments:
public extension UIView {
public func distributeSubviewsInACircle(xOffset: CGFloat, yOffset: CGFloat) {
let center = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
let radius: CGFloat = self.bounds.size.width / 2
let angleStep: CGFloat = 2 * CGFloat(Double.pi) / CGFloat(self.subviews.count)
var count = 0
for subview in self.subviews {
let xPos = center.x + CGFloat(cosf(Float(angleStep) * Float(count))) * (radius - xOffset)
let yPos = center.y + CGFloat(sinf(Float(angleStep) * Float(count))) * (radius - yOffset)
subview.center = CGPoint(x: xPos, y: yPos)
count += 1
}
}
}
source
share