Tag Archive for 'objective-c'

UIView Frames and Bounds

All UIViews have frame and bounds properties which define their dimensions. The similarity of these properties can cause some confusion when attempting to determine which property to use to calculate view sizes or reposition views.

The frame of a view is given in the coordinates of its superview so a view controller’s view’s frame will include offsets for the status bar, navigation bar, or tab bar. The bounds of a view are given in the view’s own coordinate system which does not include these elements.

In addition the frame property’s values are undefined if the view has any transform other than the identity transform. Rotating a view into landscape mode applies a transform to the view so it is not safe to rely on frame values for an app in landscape mode.

When a transform is applied to a view it does not apply to any subviews. A UIViewController or custom UIView subclass is therefore free to adjust the frames of its subviews to reposition them regardless of the device’s orientation. For example a UIView class can change the frames of its subviews within its layoutSubviews method no matter what transform might have been applied to the parent view.
Each view’s frame will position it relative to its parent view so an origin of (0, 0) will always place a subview in the top left corner of its parent view.

See the images below for examples Continue reading ‘UIView Frames and Bounds’

Testing iPhone View Controllers

I have been writing tests around my iPhone apps’ view controllers in order to follow the same TDD practices we use in other environments. Writing tests first has changed the way I structure my code in a couple of ways which I think offer immediate and emergent benefits for my applications. Most of an iPhone application’s business logic is implemented in its view controllers. Testing those controllers is therefore a priority if I want to have a well tested application.
Below are some examples of the sort of tests I have written for my view controllers using GTM, Hamcrest, and OCMock (our iPhone Unit Testing Toolkit). Hopefully this can serve as a starting point for the tests you could be writing for your own projects.
Continue reading ‘Testing iPhone View Controllers’

Multiple Environments in an iPhone Project

I’ve been following a Rails convention and defining multiple environments for my iPhone projects. This allows me to quickly switch application wide settings when running a test, development, or production version of the app.
Continue reading ‘Multiple Environments in an iPhone Project’

Code Formatting in Xcode

Working in Xcode I almost forgot how much I like IntelliJ’s code formatting tools. Luckily for me Mike Smith recently pointed out that UniversalIndentGUI can be used to setup your own code formatting in Xcode.
Continue reading ‘Code Formatting in Xcode’

Resizing views around the keyboard

Most of my iPhone apps need to accept keyboard input somewhere and that means I need to reposition my UI elements such that the keyboard does not hide the field the user is typing in. The iPhone Application Programming Guide offers one example of Moving Content That Is Located Under The Keyboard but this is not necessarily the best solution.
Continue reading ‘Resizing views around the keyboard’

Loading UITableViewCells from a nib file

While working on iPhone applications I have found it useful to load as much of the UI layout and styling from nib files as possible. As a result I often want to load the cells for a table view from a nib file and created a factory class to handle this behavior every time I want to reuse it.
When I need to use a custom cell in a table view I add a cell factory as a property on the view’s controller and select a cell to use by setting that property when constructing the controller. This often allows me to reuse a single factory across multiple controllers so I don’t have to repeat myself by including duplicate references to the same cell nib in every controller.

Continue reading ‘Loading UITableViewCells from a nib file’

iPhone gesture recognition

I recently needed to support some basic gesture recognition in an iPhone app. As it turns out this is a little bit tricky to do when the touches occur within the same responder chain as a view which already responds to touches (like a UIScrollView or UIWebView which might need to scroll or zoom in addition to handling my custom gestures).
Continue reading ‘iPhone gesture recognition’

Resizing UILabel to fit text

UILabels are often the most convenient way to display simple text in an iPhone app however I often want a label to display variable text which may need to wrap to multiple lines. Simply increasing the height of the UILabel doesn’t help because a single line of text will be centered vertically within the label’s bounds. Instead I need to computer the size of the text I want to render and update the label’s frame’s height accordingly.

In most cases I want to adjust the size of not only a label but the view containing that label as well so I wrote a category to resize labels and their parent views.

Continue reading ‘Resizing UILabel to fit text’