June 2nd, 2009
I was having a hell of a time trying to get my UITableView to resize itself after the iPhone keyboard displayed itself. After being just a little surprised that the iPhone doesn’t resize the underlying UIView for free I figured it was up to me to do resize.
Firstly add a few variables and method declares into your ViewController.h header file:
Boolean keyboardIsShowing;
CGRect keyboardBounds;
- (void)resizeViewControllerToFitScreen; |
Now we need to register for the UIKeyboardWillShowNotification and the UIKeyboardWillHideNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; |
And these notifications need somewhere to go:
#pragma mark -
#pragma mark Keyboard Handling
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue *keyboardBoundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
[keyboardBoundsValue getValue:&keyboardBounds];
keyboardIsShowing = YES;
[self resizeViewControllerToFitScreen];
}
- (void)keyboardWillHide:(NSNotification *)note {
keyboardIsShowing = NO;
keyboardBounds = CGRectMake(0, 0, 0, 0);
[self resizeViewControllerToFitScreen];
} |
And now add the magic method to resize the view:
- (void)resizeViewControllerToFitScreen {
// Needs adjustment for portrait orientation!
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGRect frame = self.view.frame;
frame.size.height = applicationFrame.size.height;
if (keyboardIsShowing)
frame.size.height -= keyboardBounds.size.height;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = frame;
[UIView commitAnimations];
} |
And super importantly de-register the ViewController from those notifications.
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
} |
See how this all comes together?
1. We register for Notifications when the Keyboard shows/hides,
2. We react when the keyboard is shown/hidden,
3. The resizeViewControllerToFitScreen method handles our resize, including animating the underlying view so it looks pretty.
There’s a few caveats:
1. I’ve not tested on Landscape mode, I’m pretty sure this will fail.
2. UIKeyboardWillShowNotification can get fired every time you enter a textbox (as I’ve only one textbox it’s not a problem for me). So you might need to look at using …TextDidBeginEditing/…TextDidEndEditing or maintaining state differently so that the view isn’t jumping all over the place.
Posted in Apple, Cocoa | 1 Comment »
May 22nd, 2009
One little gripe I’ve always had about in OS X is the apparent waste of space either side of the Dock (assuming you have it centred at the bottom of the screen).
Geektool to the rescue!

Here’s the little scripts that I use to get the weather (just change the region code ASXX0016 to your region from weather.yahoo.com. Your region code will be found in the yahoo URL):
curl --silent "http://xml.weather.yahoo.com/forecastrss?p=ASXX0016&u=c" | grep -E '(Current Conditions:|C<BR)' | sed -e 's/Current Conditions://' -e 's/<br \/>//' -e 's/<b>//' -e 's/<\/b>//' -e 's/<BR \/>//' -e 's/<description>//' -e 's/<\/description>//' |
curl --silent "http://xml.weather.yahoo.com/forecastrss?p=ASXX0016&u=c" | grep -E '(High:)' | sed -e 's/<BR \/>//' -e 's/<b>//' -e 's/<\/b>//' -e 's/<BR \/>//' -e 's/<br \/>//' |
And the script for Memory usage, HDD space and Uptime:
top -l 1 | awk '/PhysMem/ {print $10}'
df -hl | grep 'disk0s2' | awk '{print $4}'
echo "Uptime: "`uptime | awk '{print "" $3 " " $4 " " $5 }' | sed -e 's/.$//g';` |
You might also be interested to check out other uses of Geektool on Flickr
To Apple: Let us put Widgets on the Desktop too please (without having to use a hack).
Posted in Apple | 1 Comment »
May 21st, 2009
I don’t like initialising my UIViewControllers and their associated nib explicitly using a string
FlipsideViewController *viewController = [[FlipsideViewController alloc]
initWithNibName:@"FlipsideView" bundle:nil]; |
But would rather my UIViewControllers know how to load themselves. This reduces the possibilities of typos, allows for only 1 place in code where your nib is referenced by name, and just looks better.
For example in FlipsideViewController.m I would add:
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:@"FlipsideView" bundle:nibBundleOrNil])) {
// Initialization code
}
return self;
} |
Now you can simply initialise the ViewController using
FlipsideViewController *viewController = [[FlipsideViewController alloc]
init]; |
Posted in Uncategorized | No Comments »
May 20th, 2009
Mozketo blog is written by Ben Clark-Robinson about Cocoa development, Apple and my adventures in .net. (A strange mix, yes).

I am a c# .net software engineer by day, though once the sun sets low below the horizon and the Apple startup sequence chimes I am transformed into a Cocoa & Cocoa Touch engineer.
I have a diploma in IT and a degree in IT major Software Engineering.
Bits and pieces i’ve worked on
Websites: Mozketo (this site) or WGA.
Web applications: Servizio (Windows Mobile)
Desktop application: Membership

Home computer setup
Macbook Pro with external 22″ Benq LCD, bluetooth wireless Apple keyboard, MS Notebook Mouse 5000, and my well loved iPhone 3G.
Favoured Apple Software
CSSEdit
TextMate
Cyberduck
Espresso
Aperture
Voodoo Pad
Littlesnapper – Quicksnapper profile
Server related
Using a Joyent shared hosting server and Wordpress with a custom Mozketo template.
Coffee
Everything from instant (@work) to a cappuccino or iced mocha (@home).
Posted in Uncategorized | No Comments »
March 12th, 2009
I’m trying to find the best Bluetooth Mouse for Apple Mac OS X. I really like being able to just turn on a mouse and boom it works without having to plug in a dongle. So I’ve compiled a mini-review of the Bluetooth mice I’ve tried to date and hoping you could help me expand the list. Read the rest of this entry »
Posted in Uncategorized | 1 Comment »
February 3rd, 2009
I really wanted to get a nice Grid Layout using CAConstraint, but I found the situation getting very complex very fast as each Cell needed to be able to reference the location of the last Cell and whether or not the Cell needed to drop down to a new Row, and at times needed to also reference the @”superlayer”. There has to be a better way, right?

After a little digging around I was able to find an amazing Hot Chocolate blog post which showed a fantastic trick when laying out the Cells without having to reference the prior Cell. This approach takes advantage of knowing the Cell Row/Column position plus using the Scale: attribute of the CAConstraint on the @”superlayer” and it works like a charm.
[CAConstraint constraintWithAttribute: kCAConstraintWidth
relativeTo: @"superlayer"
attribute: kCAConstraintWidth
scale: 1.0 / columns
offset: 0]]; |
Want to change the number of Cells in the Grid? Modify these variables in this method:
- (void)layoutCellsInGridLayer:(CALayer *)layer {
int columns = 6;
int rows = 6;... |
Also to give the project a little bit of “wow” here’s some random Y rotation on a Cell Layer:
- (void)setupFlipAnimationOnLayer:(CALayer *)layer {
float duration = (float)frandom(0.5, 5.0);
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation.fromValue = [NSNumber numberWithDouble:-1.0f * M_PI];
animation.toValue = [NSNumber numberWithDouble:1.0f * M_PI];
animation.duration = duration;
animation.repeatCount = 1e100f;
animation.beginTime = CACurrentMediaTime() + frandom(0.1, 30);
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[layer addAnimation:animation forKey:@"rotationY"];
} |
To get the nice 3D perspective look the project needs the following code:
- (void)setupPerspectiveWithX:(float)x andY:(float)y {
CATransform3D transform = CATransform3DMakeRotation(x, 0, 1, 0);
transform = CATransform3DRotate(transform, y, 1, 0, 0);
float zDistance = -450;
transform.m34 = 1.0 / -zDistance;
gridLayer.sublayerTransform = transform;
} |
And one last final note: take a look in createCellInParentLayer: and you will see layer.contents. If you can provide your own image the layer will automatically Fill (layer.contentsGravity = kCAGravityResizeAspectFill;) and Mask (layer.masksToBounds = YES;) to make it fit.
You can grab the code here or download the binary here (requires Leopard).
Tags: CAConstraint, Core Animation
Posted in Cocoa | 3 Comments »
February 2nd, 2009
So you’re having problems with CAConstraint? It seems like no matter what you do the Constraint isn’t being applied to the Sublayer? And you’re CALayers are appearing in weird positions? Or not appearing at all?
Well the answer for me is surprisingly simple (and embarrassing)
1
2
3
4
| [sublayer addConstraint:
[CAConstraint constraintWithAttribute:kCAConstraintMidX
relativeTo:@"superlayer"
attribute:kCAConstraintMidX]] |
Now see that that @”superlayer”? Notice that it’s not a Uppercase “L” as I was using “superLayer” where upon Core Animation was unable to find the layer “superLayer” and therefore have nothing to do with the constraints.
Tags: CAConstraint, Core Animation
Posted in Cocoa | No Comments »
January 10th, 2009
Here’s another VS2008 theme that I tweaked from the theme “Dusk” which is offered by default in Apple’s Xcode.
While I’ve versioned it 1.0 I’d say that this theme is still a work in progress as I’ve not correctly set the colours for CSS, XML and other items.

And you may of course download it from here. Feel free to play with it, keep it, throw it away, and I’m more than happy to see your creations.
Take a look at the vs2008 tag for more themes.
Tags: vs2008
Posted in Themes | No Comments »
January 5th, 2009
Today I did a little talk at Cocoaheads Brisbane, Australia. It was on Core Animation and I made a very very very very very simple application which would rotate a single image using CATransform3DIdentity to give it a nice perspective 3D rotation look.
Here’s the full Xcode 3.1 project: Download.
If you’re interested solely in the transformation here’s the code:
float zDistance = 850;
CATransform3D sublayerTransform = CATransform3DIdentity;
sublayerTransform.m34 = 1.0 / -zDistance;
subLayer.transform = sublayerTransform;
CABasicAnimation *flipAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
flipAnimation.toValue = [NSNumber numberWithDouble:1.0f * M_PI];
flipAnimation.autoreverses = YES;
flipAnimation.duration = 2.0f;
flipAnimation.repeatCount = 1e100f;
flipAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[subLayer addAnimation:flipAnimation forKey:@"flip"]; |
Where subLayer == your Core Animation layer you want to rotate.
Reference: Core Animation for Mac OS X and the iPhone and Mike Lee.
Tags: Core Animation, Xcode
Posted in Talks | No Comments »
December 31st, 2008
I’m in a state of flip-flop aka indecision as I’m trying to find the perfect typeface to use while in Textmate so I decided to turn to the Internets for opinions and recommendations.
So open up your Internet megapipes to 11, take a read and compare the screenshots provided and please leave a comment Read the rest of this entry »
Tags: Typography
Posted in Themes | 2 Comments »