[uikit] CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift

After converting code to latest swift 3.0 I am shown this error.

enter image description hereenter image description here

Also tell me solution for CGSize = CGSizeMake(0,0)

static var frameAtStartOfPan: CGRect = CGRectZero
static var startPointOfPan: CGPoint = CGPointZero

Which is also unavailable.

This question is related to uikit swift3 xcode8 ios10

The answer is


Try the following:

var myToolBar = UIToolbar.init(frame: CGRect.init(x: 0, y: 0, width: 320, height: 44))

This is for the swift's latest version


You can use this with replacement of CGRectZero

CGRect.zero

Quick fix for CGRectMake , CGPointMake, CGSizeMake in Swift3 & iOS10

Add these extensions :

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)
    }
}

Then go to "Find and Replace in Workspace" Find CGRectMake , CGPointMake, CGSizeMake and Replace them with CGRect , CGPoint, CGSize

These steps might save all the time as Xcode right now doesn't give us quick conversion from Swift 2+ to Swift 3


If you want to use them as in swift 2, you can use these funcs:

For CGRectMake:

func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
    return CGRect(x: x, y: y, width: width, height: height)
}

For CGPointMake:

func CGPointMake(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
    return CGPoint(x: x, y: y)
}

For CGSizeMake:

func CGSizeMake(_ width: CGFloat, _ height: CGFloat) -> CGSize {
    return CGSize(width: width, height: height)
}

Just put them outside any class and it should work. (Works for me at least)


For CGSize

CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height)

Swift 3 Bonus: Why didn't anyone mention the short form?

CGRect(origin: .zero, size: size)

.zero instead of CGPoint.zero

When the type is defined, you can safely omit it.


In Swift 3, you can simply use CGPoint.zero or CGRect.zero in place of CGRectZero or CGPointZero.

However, in Swift 4, CGRect.zero and 'CGPoint.zero' will work


The structures as well as all other symbols in Core Graphics and other APIs have become cleaner and also include parameter names. https://developer.apple.com/reference/coregraphics#symbols

CGRect(x: Int, y: Int, width: Int, height: Int)
CGVector(dx: Int, dy: Int)
CGPoint(x: Double, y: Double)
CGSize(width: Int, height: Int)

CGPoint Example:

let newPoint = stackMid.convert(CGPoint(x: -10, y: 10), to: self)

CGVector Example:

self.physicsWorld.gravity = CGVector(dx: 0, dy: gravity)

CGSize Example:

hero.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 16, height: 18))

CGRect Example:

let back = SKShapeNode(rect: CGRect(x: 0-120, y: 1024-200-30, width: 240, height: 140), cornerRadius: 20)

Apple Blog

Swift syntax and API renaming changes in Swift 3 make the language feel more natural, and provide an even more Swift-y experience when calling Cocoa frameworks. Popular frameworks Core Graphics and Grand Central Dispatch have new, much cleaner interfaces in Swift.


In Swift 4 it works like below

collectionView.setContentOffset(CGPoint.zero, animated: true)

Examples related to uikit

CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift How to trap on UIViewAlertForUnsatisfiableConstraints? Changing Placeholder Text Color with Swift Move view with keyboard using Swift How to use UIVisualEffectView to Blur Image? preferredStatusBarStyle isn't called How to change Navigation Bar color in iOS 7? UICollectionView spacing margins How to programmatically get iOS status bar height Get UIScrollView to scroll to the top

Examples related to swift3

Xcode 9 Swift Language Version (SWIFT_VERSION) Convert NSDate to String in iOS Swift Waiting until the task finishes Type of expression is ambiguous without more context Swift Removing object from array in Swift 3 Generate your own Error code in swift 3 Swift 3: Display Image from URL add Shadow on UIView using swift 3 how to open an URL in Swift3 Correctly Parsing JSON in Swift 3

Examples related to xcode8

Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3 Xcode: Could not locate device support files Xcode 8 shows error that provisioning profile doesn't include signing certificate Xcode error: Code signing is required for product type 'Application' in SDK 'iOS 10.0' Correctly Parsing JSON in Swift 3 How can I delete derived data in Xcode 8? CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift How to create dispatch queue in Swift 3 Hide strange unwanted Xcode logs Can I delete data from the iOS DeviceSupport directory?

Examples related to ios10

Xcode error: Code signing is required for product type 'Application' in SDK 'iOS 10.0' iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash Registering for Push Notifications in Xcode 8/Swift 3.0? CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift disable viewport zooming iOS 10+ safari? Hide strange unwanted Xcode logs Transport security has blocked a cleartext HTTP