Restricting Orientations in different view controllers

At some point you may want to restrict certain device orientations to certain view controllers. Its quite easy to do from your App Delegate.

First add a new variable to your AppDelegate.swift file:

   var allowableOrientations: UIInterfaceOrientationMask = .All

Next, implement the application:supportedInterfaceOrientationsForWindow function in the same file:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    return allowableOrientations;
}

This will be called whenever you rotate the device to determine if that orientation is allowed. It doesn’t, however, force a rotation. We can implement another function in the same file to handle setting the allowableOrientations file and making sure the current orientation is one of those:

    func setAllowableOrientations(orientationMask:UIInterfaceOrientationMask,
                               defaultOrientation:UIInterfaceOrientation) {
        self.allowableOrientations=orientationMask;
        if ((orientationMask.rawValue & UInt(UIDevice.currentDevice().orientation.rawValue)) == 0) {
            UIDevice.currentDevice().setValue(defaultOrientation.rawValue, forKey: "orientation")
        }
    }

To use it, simply call this function from your View Controller’s viewDidLoad function, similar to this:

    (UIApplication.sharedApplication().delegate as! AppDelegate).setAllowableOrientations(.Landscape,defaultOrientation: .LandscapeRight)

That’s it!

Fixing the status bar visible after photo roll showing bug on iOS7/iPad

In iOS7 disabling the status bar is a bit more complex than it was in iOS6. (We have to add a row call it “View controller-based status bar appearance” and set it to boolean NO in the Target Info properties tab.)

Unfortunately, there’s also a bug on the iPad (up to 7.1 as of this writing) that makes the status bar visible again after the photo roll is shown. Luckily, there’s a pretty easy fix.

First, make sure your UIViewController subclass (the one that is already a UIImagePickerControllerDelegate) is a UINavigationControllerDelegate as well. (It should be anyway to get rid of the warnings in XCode.) Then add the following UINavigationControllerDelegate function to that subclass:

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

That’s it!

Debugging no-touch zones

When converting some iOS apps from iOS6 to iOS7, I came across a very strange bug. I had an app with tons of views and view controllers, and on the right side of the screen, touches wouldn’t be recognized. (For buttons, gestures, scrolling, etc.) It was quite obvious that one of the views frame’s wasn’t covering that part of the screen. The easy way to figure out which view it is is to add:

    self.view.clipsToBounds=true;

inside each viewDidLoad, verify that the dead zone is no longer visible, and then slowly remove the clipsToBounds calls one at a time until the dead zone is visible again, and you’ve found the offending view! Then take whatever steps you need to adjust that view properly.

In my case, a view had a frame (0,0+768,1024) for a landscape view. Changing this to (0,0+1024,768) resolved the issue. Oddly enough, either sized frame didn’t cause iOS6 to have issues, just iOS7.

No touches on bottom of iPhone 5 screen

If you ever convert an old project to work with the iPhone 5 (or future larger devices), you may encounter a situation where everything displays correctly on-screen, but for some reason, you’re not getting any touches in the lower 88 pixels. (The extra height of the iPhone 5 screen in X-Code.)

If you’re using a MainWindow.xib file, this is most likely caused because you haven’t selected ‘Full Screen At Launch’ for the Window, so it uses your original size for the window (480×640). (Even though it uses your original size for the window, the larger views will ‘leak’ and the entire screen will be filled, but any input on the lower section will be masked, so you won’t get events there.)

Creating, printing and emailing high resolution PDF’s in iOS

Many kinds of business software require some form of PDF output. The easiest way in iOS to create PDF’s is using Interface Builder and the UIKit.

Generate a PDF using UIKit

First, create a blank new empty user interface file in XCode. I’ll assume you called it PDFView.xib. Make whichever view controller will be generating the PDF the owner.

Add the main pdf view, and make it 612×792, which Apple says is the standard size for a full 8.5×11 sheet of paper. (You’ll need it a standard size if you want to print, since AirPrint doesn’t allow you to shrink larger pdf’s to fit, instead cropping off the sides that are too big or printing the page tiled across multiple pages.) Assign the main view to an UIView IBOutlet in your owner view controller; I’ll assume its called PDFView1.

In the main pdf view, lay out all your UI elements as UIViews (to group), UIImageViews, and UILabels. You can add other UI elements if you want, but most standard documents can be created with only those two.

You can add more views to the file if you want to add more pages to your PDF.

When you want to create your PDF, you would do something like this:

- (NSData*)generatePDF {
   NSMutableData * pdfData=[NSMutableData data];

   [[NSBundle mainBundle] loadNibNamed:@"PDFView" owner:self options:nil];

   // by default, the UIKit will create a 612x792 page size (8.5 x 11 inches)
   // if you pass in CGRectZero for the size
   UIGraphicsBeginPDFContextToData(pdfData, CGRectZero,nil);
   CGContextRef pdfContext=UIGraphicsGetCurrentContext();

   // repeat the code between the lines for each pdf page you want to output
   // ======================================================================
   UIGraphicsBeginPDFPage();

   // add code to update the UI elements in the first page here
	
   // use the currently being outputed view's layer here	
   [self.PDFView1.layer renderInContext:pdfContext];

   // end repeat code
   // ======================================================================

   // finally end the PDF context.
   UIGraphicsEndPDFContext();

   // and return the PDF data.
   return pdfData;
}

Pretty simple eh?

Saving/Printing/Emailing the PDF.

Once you’ve generated the PDF, its easy to save/print/email.

Saving is just a matter of writing it to a file:

- (BOOL)savePDF:(NSData*)pdf toFile:(NSString*)filePath {
   if(![data writeToFile:filePath atomically:NO]){
      NSLog(@"Failed to pdf to file '%@'", filePath);
      return NO;
   }
   return YES;
}

Printing is not much harder. We simply grab the shared print controller, set up the orientation, job name, etc, set the printer’s printingItem to our PDF data, and present the printer controller to the user, which lets them choose which printer to use. iOS takes care of the rest.

- (void)printPDF:(NSData*)pdfData {
   UIPrintInteractionController *printer=[UIPrintInteractionController sharedPrintController];
   UIPrintInfo *info = [UIPrintInfo printInfo];
   info.orientation = UIPrintInfoOrientationPortrait;
   info.outputType = UIPrintInfoOutputGeneral;
   info.jobName=@"CadabraCorp.pdf";
   info.duplex=UIPrintInfoDuplexLongEdge;
   printer.printInfo = info;
   printer.showsPageRange=YES;
   printer.printingItem=pdfData;
		
   UIPrintInteractionCompletionHandler completionHandler =
      ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
         if (!completed && error)
         NSLog(@"FAILED! error = %@",[error localizedDescription]);
      };
   [printer presentAnimated:YES completionHandler:completionHandler];
}

Emailing is similarly simple. We just create a new MFMailComposeViewController, add the data as a pdf attachment, and present it to the user to fill in email address, etc. (We could put in defaults before we present it, but I leave that to your imagination.):

- (void)emailPDF:(NSData*)pdfData {
   MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

   picker.mailComposeDelegate = self;
   [picker setSubject:@"Sending PDF"];
   [picker addAttachmentData:pdfData mimeType:@"application/pdf" 
                    fileName:[NSString stringWithFormat:@"CadabraCorp.pdf"]];
   [picker setMessageBody:@"Here's the PDF you wanted." isHTML:YES];

   [self presentModalViewController:picker animated:YES];
   [picker release];
}

#pragma mark = MFMailComposeViewControllerDelegate function

- (void)mailComposeController:(MFMailComposeViewController*)controller 
          didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
   [self dismissModalViewControllerAnimated:YES];
   if (result==MessageComposeResultSent) {
      NSLog(@"PDF sent");
   } else {
      NSLog(@"PDF send error %@",error);
   }
}

Why does it look so pixelated?

Unfortunately, using the standard way of creating PDF’s with UIKit results in a bit of a problem… By default, UIKit uses 72dpi, and that leaves quite a bit of it looking pixelated. The only solution I’ve found is to make the View’s larger (I usually do 4x larger to make the math easy) and then shrink it when generating the PDF. This results in a higher resolution PDF that is still a full page and prints properly at 100%.

First, resize each UI element by multiplying its x/y/width/height by 4. Any fonts should have their point sizes multiplied by 4 as well. You may have to create new images for the UIImageView’s you use.

Next, modify the creation of the output PDF to scale the layer’s:

- (NSData*)generatePDF {
   NSMutableData * pdfData=[NSMutableData data];

   [[NSBundle mainBundle] loadNibNamed:@"PDFView" owner:self options:nil];

   // by default, the UIKit will create a 612x792 page size (8.5 x 11 inches)
   // if you pass in CGRectZero for the size
   UIGraphicsBeginPDFContextToData(pdfData, CGRectZero,nil);
   CGContextRef pdfContext=UIGraphicsGetCurrentContext();

   // repeat the code between the lines for each pdf page you want to output
   // ======================================================================
   UIGraphicsBeginPDFPage();

   // add code to update the UI elements in the first page here
	
   CGContextSaveGState(pdfContext);
   CGContextConcatCTM(pdfContext,CGAffineTransformMakeScale(.25, .25));

   // use the currently being outputed view's layer here	
   [self.PDFView1.layer renderInContext:pdfContext];

   CGContextRestoreGState(pdfContext);

   // end repeat code
   // ======================================================================

   // finally end the PDF context.
   UIGraphicsEndPDFContext();

   // and return the PDF data.
   return pdfData;
}

That’s it! The end result is PDF’s of exactly the same size (8.5×11) but with higher resolution text and images.

Fixing blurry UIButton’s.

Once in a while you’ll notice that your UIButton images on your iOS device and simulator seem a bit blurry, while in XCode they seem fine. One possible reason is that your png images are an odd width and/or height. This seems to be because iOS tries to center the image in your UIButton, and since half of an even number is not an integer, it has to antialias everything to fit nicely.

The fix is simple: Add an extra pixel to the width and/or height to make it an even number.