Reputation: 2274
I am trying to get the center points of all UITabBar subviews in the window base coordinate system using the following code.
func getHighlightLocations() -> [CGPoint] {
var cgpointArray = [CGPoint]()
self.tabBar.subviews.foreach {
let point = $0.convert($0.center, to: nil)
cgpointArray.append(point)
}
return cgpointArray
}
The issue i have is the coordinates returned fall outside the device screen
po cgpointArray
▿ <Array<CGPoint>>
▿ some : 6 elements
▿ 0 : (252.0, 123.3)
- x : 252.0
- y : 123.3
▿ 1 : (207.0, 844.0)
- x : 207.0
- y : 844.0
▿ 2 : (54.0, 828.5)
- x : 54.0
- y : 828.5
▿ 3 : (261.5, 828.5)
- x : 261.5
- y : 828.5
▿ 4 : (675.5, 828.5)
- x : 675.5
- y : 828.5
▿ 5 : (468.0, 828.5)
- x : 468.0
- y : 828.5
What could I be doing wrong ?, Why don't return points fall within window device screen.
Upvotes: 0
Views: 108
Reputation: 77690
I believe you want to convert from the tabbar view ... so, try it like this:
class MyTabBarController: UITabBarController {
func getHighlightLocations() -> [CGPoint] {
var cgpointArray = [CGPoint]()
tabBar.subviews.forEach {
let point = tabBar.convert($0.center, to: nil)
cgpointArray.append(point)
}
return cgpointArray
}
}
For a 3-item tab bar, I get this as a result (the first point is from the tabbar's background view):
[(187.5, 642.5), (62.5, 643.0), (187.5, 643.0), (312.5, 643.0)]
Upvotes: 3