I want to create a category on UIColor
in my app using Xcode 6. But the thing is that in Xcode 6 there is no Objective-C category file template.
Is there any option to create a category in Xcode 6?
They didn't forget. They just moved it without telling anyone.
Click File
-> New
-> File
Select Objective-C file
under Sources
in iOS
or Mac OS
respectively and Click Next
Now under File Type:
choose either Category
, Protocol
, or Extension
PS. Under File Name:
whatever you type here will be either the Category
, Protocol
, or Extension
Name.
Xcode6-Beta5 update
The interface has now changed and it's possible to add a Category directly from the New > File window.
See unmircea's answer.
I was surprised myself, and I guess because of Swift they forgot about good old Objective-C.
You have two options:
Create an Objective-C class with the category name, example UIView+Powerups
, then manually change the interface to match the one of category. Note that the snippet for the category interface and implementation is still working, so that's extra easy: type @interface-category
and @implementation-category
.
Import it from Xcode 5! Use this command:
cp -r /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File\ Templates/Cocoa\ Touch/Objective-C\ category.xctemplate /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File\ Templates/Source/
Close and reopen Xcode 6 and you'll find "Objective-C Category" in the wizard for the new file.
There is no predefined template to create category in Xcode 6 beta(for time being),they may add this option later. As a work around you can create a Cocoa Touch Class
(its not proper i know but no other way) named UIImage+Additions
(ClassName+CategoryName) and override its interface and implementation some thing like
#import <UIKit/UIKit.h>
@interface UIImage(Additions)
+(void)testMethod;
@end
#import "UIImage+Additions.h"
@implementation UIImage (Additions)
+(void)testMethod
{
}
@end
Edit
This answer was written before finding a way of creating category in the Xcode 6 beta. Check unmircea's answer for the right way of creating category
Extending unmircea's fantastic answer re: how to create a custom category to implement a custom UIColor
palette, you could create a category.
Once you've created your category (in this example, it's a category called ColorPalette
of class UIColor
), you'll have a header and an implementation file.
UIColor+ColorPalette.h
#import <UIKit/UIKit.h>
@interface UIColor (ColorPalette)
// Your custom colors
+ (UIColor *) customRedButtonColor;
+ (UIColor *) customGreenButtonColor;
@end
UIColor+ColorPalette.m
#import "UIColor+ColorPalette.h"
@implementation UIColor (ColorPalette)
// Button Colors
+ (UIColor *) customRedButtonColor {
return [UIColor colorWithRed:178.0/255.0 green:25.0/255.0 blue:0.0/255.0 alpha:1.0];
}
+ (UIColor *) customGreenButtonColor {
return [UIColor colorWithRed:20.0/255.0 green:158.0/255.0 blue:96.0/255.0 alpha:1.0];
}
To use your custom color palette, just import the header into the class where you'd like to implement your custom colors:
#import "UIColor+ColorPalette.h"
and call the color as you would a standard color like redColor
, greenColor
, or blueColor
.
Here's a link to a slightly more in-depth discussion of creating a custom palette.
Additionally, here is a tool to help you select the custom color values
You could just copy the templates you want from an older version of Xcode, I made a shell script for this:https://github.com/cDigger/AddMissingTemplates
@interface UIColor(MyCategory) ...
etc – JeremyP