Anders Brownworth

Technology and Disruption

iPhone Programming: UIGlassButton

In iPhone programming land, there are a number of "private APIs" which can be used but aren't allowed in the App Store. One such API is the UIGlassButton which, just as it's name implies, creates a translucent button like the "End Call" button in the phone app. But big fat buttons are useful elsewhere so to get around the restriction you can use a PNG image of the button instead. Use the following code temporarily in your project and run it in the simulator. You should get a file named glass-button.png in your home directory if it works. Then just use that image as a background for a custom button and you should be set.

Class theClass = NSClassFromString(@"UIGlassButton");
UIButton *theButton = [[[theClass alloc] initWithFrame:CGRectMake(10, 10, 240, 44)] autorelease];
[theButton setValue:[UIColor colorWithHue:0.267 saturation:1.000 brightness:0.667 alpha:1.000] forKey:@"tintColor"];
//[theButton setTitle:@"Continue" forState:UIControlStateNormal];
[self.view addSubview:theButton];

UIGraphicsBeginImageContext(theButton.frame.size);
CGContextRef theContext = UIGraphicsGetCurrentContext();
[theButton.layer renderInContext:theContext];

UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *theData = UIImagePNGRepresentation(theImage);
[theData writeToFile:@"~/Desktop/glass-button.png" atomically:NO];

UIGraphicsEndImageContext();

Comments (0)

Leave a Comment

Name:
Location: (city / state / country)
Email: (not published / no spam)
Comment:

No HTML is allowed. Cookies must be enabled to post. Your comment will appear on this page after a moderator OKs it. Offensive content will not be published.

Click the umbrella to submit your comment.

To create links in comments:
[link:http://anders.com/] becomes http://anders.com/
[link:http://anders.com/|Anders.com] becomes Anders.com
Notice there is no rel="nofollow" in these hrefs. Links in comments will carry page rank from this site so only link to things worthy of people's attention.