UIViewController’s prefersStatusBarHidden not working

In iOS7, there’s actually a new property for UIViewController called modalPresentationCapturesStatusBarAppearance. Apple iOS reference. Default value is NO. When you present a view controller by calling the presentViewController:animated:completion: method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller’s modalPresentationStyle value is UIModalPresentationFullScreen. By setting this … Read more

When should I initialize a view controller using initWithNibName?

-initWithNibName:bundle: is the designated initializer for UIViewController. Something should eventually call it. That said, and despite Apple’s examples (which favor brevity over maintainability in many cases), it should never be called from outside the view controller itself. You will often see code like this: MYViewController *vc = [[MYViewController alloc] initWithNibName:@”Myview” bundle:nil]; I say this is … Read more

Xcode/iOS5: Move UIView up, when keyboard appears

To move the view up, just change its center. First, keep the original one in a CGPoint property. – (void)viewDidLoad { … self.originalCenter = self.view.center; … } Then, change as needed when keyboard shows up: self.view.center = CGPointMake(self.originalCenter.x, /* new calculated y */); Finally, restore it when keyboard is hidden: self.view.center = self.originalCenter; Add animation … Read more

Getting the correct bounds of UIViewController’s view

How to do this correctly Your UIViewController subclass should override the method viewWillLayoutSubviews, see also here. When this method is called, the viewController’s view has its correct size and you can make any necessary adjustments to subviews prior to the layout pass over the subviews. Swift override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() NSLog(“bounds = \(self.view.bounds)”) } … Read more

View Controllers: How to switch between views programmatically?

There’s a nice example of switching views in Chapter 6 of Beginning iPhone Development. You can see the source code for it here: http://iphonedevbook.com/ SwitchViewController has the code to change views programatically. – (IBAction)switchViews:(id)sender { if (self.yellowViewController == nil) { YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@”YellowView” bundle:nil]; self.yellowViewController = yellowController; [yellowController release]; } [UIView beginAnimations:@”View … Read more

Custom Animation for Pushing a UIViewController

I use the following function (added to UINavigationController) to customize the push animation: – (void) pushController: (UIViewController*) controller withTransition: (UIViewAnimationTransition) transition { [UIView beginAnimations:nil context:NULL]; [self pushViewController:controller animated:NO]; [UIView setAnimationDuration:.5]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationTransition:transition forView:self.view cache:YES]; [UIView commitAnimations]; } I guess you could adapt this code to do whatever animation you want.

Present modal view controller in half size parent controller

You can use a UIPresentationController to achieve this. For this you let the presenting ViewController implement the UIViewControllerTransitioningDelegate and return your PresentationController for the half sized presentation: func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? { return HalfSizePresentationController(presentedViewController: presented, presenting: presentingViewController) } When presenting you set the presentation style to .Custom and set … Read more