UIDatePicker Height Inconsistency at Runtime [#iOSDev, #SwiftLang]

Apple needs to fix this one.

There is an inconsistency in the way that the UIDatePicker is rendered in Storyboards in Xcode vs. their rendering at runtime. This problem set me back longer than it should have this morning, since its effect was tricky to diagnose. The basic consequence of this was that the custom UIButton I had attached to the top of the date picker (inside a UIView) was not receiving taps and for the life of me I couldn’t figure out why…

First, to diagnose the problem, I used this tip to get the UIView recursiveDescription method working in my Swift project, and I was able to get a dump of the view hierarchy. From that, I could tell that the UIView I had created as a container was at a Y value of -54, and was therefore outside the bounds that I had set for the custom view, and was not receiving tap events.

Then I stumbled on this Stack Overflow post by Florian Mielke, which suggested that there is a typo-bug somewhere in Xcode or iOS in the rendering of UIDatePicker in a Storyboard vs. Runtime:

iphone – iOS 7 UIDatePicker height inconsistency? – Stack Overflow

“I can confirm that using UI(Date)Picker in storyboards has a different height (162.0) than in “reality” (216.0). Therefore you have to adjust the space to container view to fit that ‘real’ date picker height or try to solve it using auto-layout.”

Reconciling that with the output from recursiveDescription totally agreed with this, and once I increased the size of my custom view, taps were once again being received by the Done button!

How to Know When Apple Finally Gets iCloud Right [#iosdev #icloud]

Gus Mueller of Flying Meat Software offers his opinions on what Apple must do to get iCloud right for developers:

The Shape of Everything: How to Know When Apple Finally Gets iCloud Right

But how are we going to know Apple has finally fixed iCloud syncing for developers and is really serious this time? And I’m not just talking about Core Data syncing, I’m also talking about the APIs developers are given to push document data back and forth. The broken stuff, the things developers laugh at Apple about and have given up on.

How to retrieve the HTML contained inside a UIWebView in iOS [#iosdev]

Recently on a project I needed to see what was going on inside the UIWebView that I was working with. Turns out it is very simple to retrieve and then log the HTML contained inside the UIWebView, using the stringByEvaluatingJavaScriptFromString method of the UIWebView, coupled with a very simple JavaScript call…

- (void)logDocumentHtml {
    NSString *html = [self.chatWebView stringByEvaluatingJavaScriptFromString: @"document.documentElement.innerHTML"];
    NSLog(@"document HTML:\n%@\n", html);

Hope this helps!

#iOSDev Tip: Unblock events caused when adding subviews to your UIButtons

Recently, I was having an issue I was having with subviews added to my UIButton objects that I was customizing. The buttons would behave fine if they were standard rounded rect buttons, but the moment I added a UIView and some UILabels to it, the taps stopped responding.

But I was able to resolve it, thanks to the tip provided here:

iPhone: Subviews in UIButtons block the touch, unless…

The solution boils down to something very simple and it makes sense when you think about it, but at first it seems strange:

  • All you need to do is set userInteractionEnabled and exclusiveTouch to NO on the items that you want to add as subviews to your UIButton (and don’t set those properties to NO for the UIButton itself).

Once you turn off user interaction and exclusive touch properties on the subviews, your events will be sent to the underlying button (or at least that’s how I like to think of it happening…).

I initially read the post wrong and set them to YES and couldn’t figure out why it wasn’t working. Then I went back and looked at the comments, which pointed out where I went wrong.

Hope this helps!