22 posts with this tag

Disable the menu for a textfield

November 17, 2011

I ran across the scenario where I wanted to disable the paste functionality on some textfield in my application. I've did this many times before and quickly added the following method to my viewcontroller: -(BOOL)canPerformAction:(SEL)action withSender:(id)sender { if ([UIMenuController sharedMenuController]) { [UIMenuController sharedMenuController].menuVisible = NO; } return NO; } While the above method had the desired effort in iOS4.x, in iOS5 I was still presented with the opt…

Hide the tab bar on certain view controllers

September 30, 2011

I found it surprisingly tricky to track down how to hide the tabbar on certain view controllers without ending up with a white area where the tabbar would have been. What you need to do is set the tabbar hidden property on the UIViewController you are about to push rather than on the UITabbarController: UIViewController *hideTabbarViewController = [[UIViewController alloc] init]; hideTabbarViewController.hidesBottomBarWhenPushed = YES; [[self navigationController] hideTabbarViewController animated:YES]; Th…

Loading bundle images in a UIWebView

September 13, 2011

I ran into this problem were I could not get a bundle image to appear in a UIWebView. It turns out that in order to enable this functionality you need to tell the webview where to look for these images, in the below example we tell it to use the main bundle: NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath]; UIWebView *webview = [[UIWebView alloc] initWithFrame:self.view.frame]; [webview loadHTMLString:self.content baseURL:bundleBaseU…

Limiting input

April 28, 2011

A common scenario in iOS development is to create a textfield that will only allow a certain number of characters to be entered. We can implement this constraint by: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { BOOL validCharactersInRange = YES; if (![string isEqualToString:@""]) { NSUInteger testLength = textField.text.length + string.length + range.length; validCharactersInRange = (testLeng…

Setting the return key

April 25, 2011

When I wanted to add a Done/Next key to the keyboard I found it surprisingly hard to find out how this was possible. After much research I stumbled upon the solutions that you go through the textfield to set the button. Done - (void)textFieldDidBeginEditing:(UITextField *)textField { textField.returnKeyType = UIReturnKeyDone; } Next - (void)textFieldDidBeginEditing:(UITextField *)textField { textField.returnKeyType = UIReturnKeyNext; } Skip onto next field - (BOOL)textFieldShouldReturn:(UITextF…

Add a background image to the navigation bar

March 28, 2011

Recently I was asked to add a pale (off-white) color as the background color of a navigation bar, which I thought would just be a case of updating the tintColor property . However when I did this instead of getting the gradient effect that we were after all I ended up with was a solid off-white color. After iOS 5.0 iOS 5.0 made it so easy to set the navigation bar background image: - (id) initWithRootViewController:(UIViewController *)rootViewController navigationBarBackgroundImage:(UIImage *)background…