CGRectMake, CGPointMake, CGSizeMake, CGRectZero and CGPointZero were removed. Use struct initializers like CGRect(x:y:width:height), CGPoint(x:y:), CGSize(width:height) and the static property .zero for zero values.Why this happens
After migrating a project to Swift 3 in Xcode 8 you will see errors like CGRectMake is unavailable in Swift, CGPointMake is unavailable in Swift, CGSizeMake is unavailable in Swift, and CGRectZero is unavailable in Swift / CGPointZero is unavailable in Swift.
These were C-style helper functions and global constants from Core Graphics. Swift 3 modernized the import of Core Graphics and UIKit as part of the Grand Renaming. The Make functions were removed in favor of Swift struct initializers, and the global Zero constants were replaced by a static .zero property on each type.
The same change applies to all later Swift versions. If you open old Swift 2.x code or copy Objective-C snippets that use CGRectMake(0, 0, 100, 100) or CGSize = CGSizeMake(0,0), the compiler has no replacement function to call. You must use the Swift initializer syntax instead. The types themselves (CGRect, CGPoint, CGSize) did not change, only how you create them.
How to fix it
The fix is a direct syntax replacement. No import changes or build settings are needed. Replace every Make call with its initializer and every Zero constant with .zero.
1. Replace CGRectMake, CGPointMake, CGSizeMake with Swift initializers
This is the recommended fix and what the Swift migrator expects.
Before (Swift 2 / Objective-C style):
let rect = CGRectMake(0, 0, 100, 100)
let point = CGPointMake(0, 0)
let size = CGSizeMake(100, 100)
After (Swift 3 and later):
let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
let point = CGPoint(x: 0, y: 0)
let size = CGSize(width: 100, height: 100)
All three initializers take CGFloat. You can pass Int or Double literals and Swift will convert them, so CGRect(x: 0, y: 0, width: 100, height: 100) works without explicit CGFloat(...) casts.
CGRect also has an initializer that takes an origin and a size, which is useful when you already have a point and size:
let rect2 = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 100, height: 100))
let rect3 = CGRect(origin: .zero, size: CGSize(width: 100, height: 100))
For the example from the error report:
// Before
static var frameAtStartOfPan: CGRect = CGRectZero
static var startPointOfPan: CGPoint = CGPointZero
let size = CGSizeMake(0, 0)
// After
static var frameAtStartOfPan: CGRect = .zero
static var startPointOfPan: CGPoint = .zero
let size = CGSize(width: 0, height: 0)
// or simply
let size2 = CGSize.zero
If the type is already known from context you can use the shorthand .zero or omit the type name. If not, write the full type:
let r1: CGRect = .zero
let r2 = CGRect.zero
let p1: CGPoint = .zero
let s1: CGSize = .zero
2. Replace CGRectZero, CGPointZero, CGSizeZero with .zero
The global constants were removed:
CGRectZero->CGRect.zeroCGPointZero->CGPoint.zeroCGSizeZero->CGSize.zero
All three mean the same thing: origin or size with all components set to 0.
// Before
static var frameAtStartOfPan: CGRect = CGRectZero
static var startPointOfPan: CGPoint = CGPointZero
if size.equalTo(CGSizeZero) { }
// After
static var frameAtStartOfPan: CGRect = CGRect.zero
static var startPointOfPan: CGPoint = CGPoint.zero
if size == CGSize.zero { }
You can also compare directly with == now that these types conform to Equatable:
if rect == .zero { }
if point == .zero { }
For CGSize = CGSizeMake(0,0) specifically, use either form:
let sizeA = CGSize.zero
let sizeB = CGSize(width: 0, height: 0)
Both create a size with width = 0 and height = 0. Prefer CGSize.zero when you mean “no size” and the initializer when the zeros are just initial values you will change.
3. Migrate a large codebase quickly
If you have hundreds of occurrences, use Xcode’s Find and Replace in Workspace instead of fixing one by one.
- Open Find > Find and Replace in Workspace (
Shift-Command-F). - Set Find to
CGRectMake, Replace toCGRectand replace all. Repeat forCGPointMake->CGPointandCGSizeMake->CGSize. - Then replace
CGRectZero->CGRect.zero,CGPointZero->CGPoint.zero,CGSizeZero->CGSize.zero. - Build and fix any remaining argument label issues. The new initializers require labels:
CGRect(x:y:width:height:), notCGRect(0,0,100,100).
You may see a workaround online that adds compatibility extensions to bring back the old call style:
extension CGRect {
init(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) {
self.init(x: x, y: y, width: width, height: height)
}
}
extension CGSize {
init(_ width: CGFloat, _ height: CGFloat) {
self.init(width: width, height: height)
}
}
extension CGPoint {
init(_ x: CGFloat, _ y: CGFloat) {
self.init(x: x, y: y)
}
}
This lets CGRect(0,0,100,100) compile without labels. It can unblock a large migration temporarily, but it is not idiomatic Swift and hides the real API. Prefer doing the proper Find and Replace to CGRect(x:y:width:height:) and then remove the shim. New code and code review should use the labeled initializers and .zero.
How to prevent it
- Write new code with Swift initializers from the start:
CGRect(x:y:width:height:),CGPoint(x:y:),CGSize(width:height:)and.zero. Do not copyMakecalls from Objective-C examples without converting them. - When porting Objective-C snippets, translate
CGRectMake(a,b,c,d)toCGRect(x: a, y: b, width: c, height: d)manually. - Enable the Xcode migrator or treat warnings as errors after a Swift version bump so unavailable APIs are caught immediately.
- Search your project for
Make(andZeroafter any major Xcode/Swift update to catch leftover C-style Core Graphics code. - Avoid adding custom
init(_:_:)shims long-term. They make your codebase diverge from standard Swift and from Apple’s documentation and sample code.
FAQ
Is CGRectMake still available in newer Swift versions?
No. CGRectMake, CGPointMake, and CGSizeMake were removed in Swift 3 and have not returned. In Swift 5, 6 and later you must use CGRect(x:y:width:height:), CGPoint(x:y:), and CGSize(width:height:). The same applies to CGRectZero and CGPointZero, which are now CGRect.zero and CGPoint.zero.
What is the Swift equivalent of CGSizeMake(0,0)?
Use CGSize.zero or CGSize(width: 0, height: 0). Both produce a size with zero width and height. CGSize.zero is shorter when you mean an empty or default size. The initializer form is better when you are constructing a size from variables.
Should I add an extension to bring back CGRectMake?
Only as a short-term migration aid if you have a very large codebase and need to get it building quickly. For maintainability, replace all calls with the official labeled initializers and .zero and delete the extension. Apple’s frameworks, documentation, and all modern Swift code use the new syntax.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.