I want to initiate an instance of UITableView
from CGRectMake
function, but when I tried to initiate it using constants instead of directly assigning a new Int
, such as:
UITableView(frame: CGRectMake(20, HEIGHT_OF_STATUS_AND_NAVIGATION_BAR + 120, tableWidth, tableHeight)
, then I got an error: Int is not convertible to CGFloat
.
So I changed it to the following code:
UITableView(frame: CGRectMake(20, (HEIGHT_OF_STATUS_AND_NAVIGATION_BAR + 120) as CGFloat, tableWidth as CGFloat, tableHeight as CGFloat)) // `tableWidth` and `tableHeight` are both of type `Int`.
, then I got an error: Cannot invoke + with an argument of type CGRect
.
Of course I can initiate it by just assigning Int
, such as CGRectMake(20 + 300 + 12, 44 + 120, 300, 480)
. But I want to use constant and variable.
So what does the error mean? And how can I fix it?
I use Xcode 6 beta 6.
The way @Martin R does works well, but later I found that there are no reasons to bother to use CGFloat()
- you can just use CGRect()
instead of CGRectMake()
in Swift, on which you can specify the argument as Int
as well as Double
or CGFloat
to all the x
, y
, width
, and height
properties as long as all the four properties are of the same type. Also, you cannot omit the name of each property which you can in the case of CGRectMake()
, but this is easier, more flexible, and considered to be the default CGRect
initializer in Swift.