Showing posts with label IOS. Show all posts
Showing posts with label IOS. Show all posts

Monday, January 11, 2016

IOS UIButton's image aspect fit mode does not work

I encountered a problem with displaying image icons in UIButton elements in IOS. As shown in the screenshot below, the icon images are stretched out disregarding the aspect fit mode set in Xcode.

After troubleshooting the issue, I found that setting the UIButton's image content mode manually to scale by aspect fit will solve the problem.

In Xcode, I link all the buttons with icons to an outlet. Then I loop through the buttons and set the content mode property, as shown in the example code listing below.


@IBOutlet var filterButtons: [UIButton]?

//...etc...

override func viewDidLoad() {
     super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
        
//Workaround to keep the button icons scaled properly
   for button in filterButtons! {
       button.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
    }
}

The image buttons are now correctly rendered as shown in the screenshot below.

Monday, December 7, 2015

How to add many bar button items to the IOS toolbar in Xcode

This is probably something IOS development newbies might encounter while designing an IOS app graphical interface. By default, when adding a Toolbar to the main storyboard, a single bar button is included as shown below.

I wanted to add more bar button items to the toolbar; and I look high and low for a way in the property panes on the right of Xcode. Eventually, I figured that the bar button items need to be dragged from the Object library and dropped onto the toolbar on the main storyboard, as shown in the screenshot below.

The screenshot below shows three bar button items on the toolbar.