Online Book Reader

Home Category

iOS Recipes - Matt Drance [15]

By Root 276 0
generates a system Done button, which is useful when presenting the controller modally. Bar button items are among those things we create all the time but are rather verbose; in this case, we’ve packaged one inside our reusable controller.

SmartWebView/PRPWebViewController.m

- (void)setShowsDoneButton:(BOOL)shows {

if (showsDoneButton != shows) {

showsDoneButton = shows;

if (showsDoneButton) {

UIBarButtonItem *done = [[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemDone

target:self

action:@selector(doneButtonTapped:)];

self.navigationItem.rightBarButtonItem = done;

[done release];

} else {

self.navigationItem.rightBarButtonItem = nil;

}

}

}

To do all this work on our behalf, PRPWebViewController needs to act as the web view’s delegate. But what if our code cares about a load failure or needs to know when the page finished loading? Delegation is a “one-to-one” relationship, so we can’t just steal the UIWebViewDelegate role from PRPWebViewController or we’ll break its functionality. So in this case, we’ll declare a new PRPWebViewControllerDelegate delegate to echo relevant events to an interested party.

SmartWebView/PRPWebViewControllerDelegate.h

@class PRPWebViewController;

@protocol PRPWebViewControllerDelegate

@optional

- (void)webControllerDidFinishLoading:(PRPWebViewController *)controller;

- (void)webController:(PRPWebViewController *)controller

didFailLoadWithError:(NSError *)error;

- (BOOL)webController:(PRPWebViewController *)controller

shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation;

@end

The autorotation method allows you to dictate the controller’s behavior based on your own UI. All of these protocol methods are optional: you have no responsibility to implement any of them. The PRPWebViewController can still act completely on its own.

You now have a self-sufficient web view that presents a smooth transition to users as its content loads. All you need to do to include web content in your apps is create a PRPWebViewController, set a URL, and display it.

Recipe 7 Customize Sliders and Progress Views

Problem

The standard look of UISlider and UIProgressViews may not match the rest of your app. Unfortunately, Interface Builder allows you to adjust only the width. What can you do to give these elements a different look?

Solution

To go beyond the basics, we need to dip into code to explore some of the available image properties and understand how to use stretchable UIImages.

UISlider has a set of properties that aren’t exposed by Interface Builder: currentMaximumTrackImage, currentMinimumTrackImage, and currentThumbImage. These properties give a lot of flexibility by letting us specify alternate images for the control. To get the most out of them, we need to understand how stretchable UIImages work (see Figure 13, Custom slider demo screen ).

Figure 13. Custom slider demo screen

* * *

We create a stretchable UIImage from an image file just like any other image, but we must also set the leftCapWidth and topCapHeight values. We do this by calling the stretchableImageWithLeftCapWidth:topCapHeight method to define the length of the sections that will remain unstretched. If an image is 100 points in width and we define the leftCapWidth as 49, then the 50th point would be the one that is stretched (or duplicated), and the remaining 50 points would remain fixed. If we then set the image length to 200, then 150 copies of the stretched point are inserted to fill out the image. As you can see, we need to pick the image and stretch points carefully so it still looks correct when it is stretched. Check out the images in the sample code. The images appear to be oddly shaped but achieve the look we want when they are stretched.

CustomSlider/Classes/CustomSliderViewController.m

UIImage* sunImage = [UIImage imageNamed:@"sun.png"];

[customSlider setThumbImage:sunImage forState:UIControlStateNormal];

We can use the thumbImage property to set a new image for the draggable element of the slider.

Return Main Page Previous Page Next Page

®Online Book Reader