UIButton adjust image size.

The Problem

I’ve been working with a few buttons that requires an image icon at work lately. Some of the icons are used in different places where the actual image are smaller then the recommended minimum touch area size. So you either get a Larger icon for the button or a smaller button.

Simple Solution

My first work around was to create a new assets for the icon with padding added. This result in multiple images are required to represent the same icon. If an icon need to be replace then multiple assets need to be created and replace in code or interface builder. Not so nice right.

Better Solution

The better way to handle this is to use


or in interface builder

Both the code block and interface builder setting will add 10 point margin on all side of the image. So the button touch area can be bigger then the image.

j j j

Xcode auto build number

All you need to do to enable this technique is to add a run script build phase any time after “Copy Bundle Resources”:

Super useful script to auto generate build number. There difference between this and the other techniques is this script does not overwrite info.plist, which require you to make another commit. I set my building number in info.plist to auto and let the script do the job.

j j j

Autolayout contraint

This week I had an idea to revive my blog. I going to writting a series of articles on let’s write Objective C. This was inspired by Youtube series like “let’s play Simcity”. So let’s get started

So my first article is on auto layout. I played with auto layout before for my work project, but I only used it for Storyboard. So this week I started to explore how to programmably use auto layout.

There are a few issues you need to keep in mind when using auto layout:

  • You can programmably add view using frame, the system will translate it to auto layout
  • Constraints added to views need to be fully realised, UIKit need to know excatly how to render the view
  • For example: (top, left, right bottom) or (top, left, width, height)
  • You MUST turn off translatesAutoresizingMaskIntoConstraints on the view
  • View need to be added to the super view before constraints can be added
  • Positional constraints are added to the parent view

Simple right

j j j