Road Test: PureLayout

Imagine there’s no translatesAutoresizingMaskIntoConstraints. It’s easy if you try. No constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: below us, above us only sky. Imagine your app not crashing because you left a “]” out of constraintsWithVisualFormat, woohoo.

*Cough* Sorry about that, folks. Let’s move on.

I’ve written before about the superiority of programmatically doing your layout code, but there is no denying that the tools for doing so are kinda kludgy. AutoLayout is powerful and flexible but we are in need – in need! – of some syntactic sugar of some kind.

You could write a lot of helper methods for this purpose, or you could just use PureLayout.

PureLayout is an open source library that makes AutoLayout a lot less painful to deal with. Say goodbye to manually setting translatesAutoresizingMaskIntoConstraints for your views, local view defines because you can’t have self.aButton in your view dictionaries, even [view addConstraints] – in short, if it’s in AutoLayout and it’s annoying, PureLayout takes care of it. You’ll do the same work in half the code.

PureLayout example

Write layout code like a frikking wizard! (From PureLayout example project)

Best of all, that’s all PureLayout does; unlike some open source libraries, it doesn’t try and do too much. If you already know AutoLayout, the learning curve is about 5 minutes.

Are there any disadvantages to PureLayout? For very complex layouts, the ability to produce 17 constraints at once with constraintsWithVisualFormat might be preferable. Other than that, I can’t see any reason why you shouldn’t use PureLayout for all your layout needs.

Posted in Uncategorized | Tagged , , , | Leave a comment

How to animate UISearchBar like in Contacts

Ever seen the animation that occurs when you tap in the search bar in Contacts? The navigation bar slides upwards, the search bar goes up and replaces it, and the Cancel button comes in from the left.

Here’s a before and after:

before animation after animation

Now, how is that done?

The first question is how to animate the Navigation Bar out of the way. This can be done with a simple frame animation as follows:

CGRect frame = self.navigationController.view.frame;
frame.origin.y -= navbarHeight;
frame.size.height += navbarHeight;
[UIView animateWithDuration:duration animations:^(){
self.navigationController.view.frame = frame;
}];

This also moves your entire view controller up, and the search bar with it.

Second step: handling the cancel button. The UISearchBar has a showsCancelButton, but if you try and animate that it comes off kinda clunky. The solution is to have the Cancel button on at all times – but set the search bar to be so wide that the cancel button is off the screen to the right. After this, a simple frame or constraint animation will get the job done.

self.searchWidth.constant = 320.f;
[self.searchBar setNeedsUpdateConstraints];
[UIView animateWithDuration:duration animations:^(){
[self.searchBar layoutIfNeeded];
}];

Naturally you will want to combine this with the Navigation Bar example :/

Next, you will probably find that the status bar appears on top of the search bar. My solution to this was to create a spacer view to appear between the navigation bar and the search bar, and adjust the height via a constraint animation. The code for this is left as an exercise for the reader.

Have fun animating, and if you have any tips or tricks to share, let me know in the comments!

Posted in Uncategorized | Tagged , , | Leave a comment

Control a fridge-opening robot via an iOS app

While at the supermarket today, I couldn’t remember whether we needed to buy meat or not. Suddenly the thought hit me – what if I had a robot that could open the fridge door for me so I could look, and I could control it remotely via an iOS app? You can hook up webcams and view them remotely and I suppose you could put a webcam in the fridge. There is only one downside to this approach: a robot with camera is much cooler than a webcam. All of which is a long-winded introduction to this: a navigable robot controlled via an iPad. Sweet.

If you did actually want to build a fridge-opening robot with an iOS interface, one way to go about it is by using an Arduino motherboard. There is a tutorial at SparkFun showing just how to do that. It involves buying an iOS app rather than writing your own, but still.

Here is a video demonstrating a fridge-opening robot with an Arduino motherboard:

Posted in Uncategorized | Tagged , , | Leave a comment

AutoLayout: Interface Builder vs code

I haven’t blogged here for 10 years but now I return … to blog mainly about iOS programming! Or so the plan is.

I’ve been an iOS developer on and off for about 5 years now and when AutoLayout first appeared I avoided it as long as I could. My first memory of trying AutoLayout with IB is getting quickly frustrated trying to add constraints that didn’t do what I wanted and the default ones doing the opposite of what I wanted. So back to code it was.

Time has passed and I’ve become not quite an expert in AutoLayout but certainly proficient in it. Every so often I read a blog post or article about the wonders of IB + AutoLayout and how it halves your time creating layouts, gives you flexibility, cooks dinner for you, does the dishes afterwards and so on. Finally I found a bit of time to kill but with no one to kill at hand, I thought I’d give the IB side of things another go.

Everyone agrees there are downsides to IB, for example with source control and merging, but do the pros outweigh the cons?

In short, no. Why? Read on dear sir and all will be revealed!

There are a number of strange quirks in the IB interface that make things unintuitive, such as:

– The first thing you notice is that all the constraints get lumped together in a haphazard order making them hard to read.

– You can click a view to reveal only the constraints applying to it but you can’t delete them here; it is necessary to do this in the massive clumped list noted prior.

– By default views don’t have meaningful labels. In my view, once they are connected the label in IB should match the property name. Instead you have to manually name everything twice.

Views named view

Views named view – that’s intuitive!

– For some reason the Pin button doesn’t allow you to pin to a superview; you must use the menus for that. Why?

Another problem is conceptual: in code, you soon learn to ignore frames entirely when constructing layouts. In IB however, it tries to honour the initial frames you place your views in, and defaults the constants in your constraints to suit. This seems bizarre to me. I thought the idea of AutoLayout was to not concern yourself with frames?

But for me, the real IB killer is building constraints with visual format language. If you want to have several views in a vertical list, in code you can do so with one formatting string:

@V:"|[view1][view2][view3][view4][view5]||"

and done!

In IB you manually have to create each constraint and then edit each of them because again the Pin button doesn’t provide enough options.

For a layout of any complexity I’d bet on coding to outrace IB hands down.

I know some prefer IB because there’s less code, but come on: we are programmers. Afraid of code we should not be.

So what do you think? Is coding layouts better or is there a magic feature in IB that I’ve overlooked?

Posted in Uncategorized | Tagged , , , | 2 Comments

*** STOP PRESS *** My Opinions Are Important moves to its own domain!

That’s right people – this blog is moving to its own domain and its own space. Check it out!

The new address is myopinionsareimportant.com

The feed address is now http://myopinionsareimportant.com/?feed=rss2

Please update your links/bookmarks/feed readers accordingly.

All the posts and comments have been ported across, so you can read them all again – and again – and again!

See you all over there.

STRANGENESS UPDATE: Hmmm, apparently new blog now thinks its feed address is http://myopinionsareimportant.com/feed. Why did it change? Not sure, but if you have problems with the feed=rss2 address, try the other one instead.

Posted in Important Announcements | 2 Comments

Great gifts for geeks

TheMolk found some excellent off-the-wall gift ideas at ThinkGeek, including a tiny remote-controlled helicopter and a tiny, yet fully functional Japanese battle tank.

tank.jpg

A great gift idea, and oh-so-handy next time World War III erupts in your home or workplace.

Posted in Interesting, but otherwise unclassifiable, Nutbar | 2 Comments

Wedding gifts for the strange: spider bowl

From Laura Zindel Ceramics, as featured on bioephemera:

spider_bowl.jpg

You are probably asking yourself why. Zindel’s explanation:

I hope that I can make art that a family member can buy to be handed down the line. Something bought on a whim, that becomes the platter for the turkey, or sits on the mantel. “Crazy old Uncle Larry bought that peculiar spider platter, and we just can’t seem to part with it”, I would like to be apart of that.

There’s also beetles, snakes and cicadas, oh my.

Posted in Nutbar | 7 Comments

Top 5 Science Experiments On Cats

** This blog has moved to myopinionsareimportant.com **

Tell me you’re not at least a little disturbed by the following:

#1: Cat-O-Vision
A team of scientists hacked into a cat’s brain and created a video of what the cat was seeing.

#2: We Have Ways Of Making You Purr
Restricting sleep to two hours a night, enforced by use of a treadmill. Sounds like a Guantanamo Bay interrogation technique, does it not? No, this is science. You’ll be pleased to know that restricting sleep results in a greater proportion of REM sleep in cats.

#3: Cats In Spaaaace
How do cats react to zero gravity? Do they handle it better than dogs? This is the scientific question of our times and only now has it been answered.

#4: I’m Not Hungry. (Bzzt.) Actually, I Am.
Scientists obtained complete control over a cat’s mind. By remote control, they could make the cat hungry, thirsty, or itchy, or even make it more aggressive or affectionate. Note that they did not force the cat to eat; they only made it desire to eat.

#5: Just Gimme One More Bowl
Do cats prefer spiked milk to ordinary milk? Only science could provide the answer. A series of experiments was performed that subjected cats to extreme stress until they became alcoholics, or masochistic, deliberately exposing themselves to repeated electric shocks.

BONUS: The Scratching Post Is Melting
Giving LSD to cats. Does this qualify as science? Discuss.

(This post is at least partly inspired by the Top 5 Group Writing Project at ProBlogger.)

Posted in Cats, Nutbar, Random cat-related Internet stuff, Science | 36 Comments

Deep & Meaningful Festival explodes onto the blogosphere

** This blog has moved to myopinionsareimportant.com ** 

Thank Robert Hruzek for this. His group writing project asked participants to write an article on a life lesson they learned from an unusual source. All told, there were 16 entries.

Here is the complete list of entries, each one finishing the phrase “What I Learned From…”

“… Teen Girl Squad”, by Markk at My Opinions Are Important
WOW! You’ll laugh, you’ll cry, you’ll cheer. Top post. Top, top post.

“… the Mt. Pinatubo Eruption”, by Ronald Huerca at Ronalfy.com
Most of this has nothing to do with volcanic eruptions, but worth the read all the same.

“… Drugs”, by Sam Brougher at Forest Azuaran
Legal drugs. Strong legal drugs that do strange things to your emotions and memory. Reminds me of a friend’s experience with oxycodeine (“The walls are melting …”).

“… a Mesquite Tree”, by Mike DeWitt at Spooky Action
You are limited only by your willingness to be bold. That, and mesquite trees are weird.

“… Drinking Starbucks Coffee”, by George Manty at Can I Make Big Money Online
The main thing I learned from drinking Starbucks coffee is not to visit Starbucks. That’s not what this post is about though :) Useful for those of you who run a business.

“… My Wife!”, by Rajaram Sethuraman at Thoughts of a Rambler
One man learns that he is a fashion disaster, among other things.

“… Having a Daughter”, by Marco Richter at FitForFreedom
How to not worry about being in debt.

“… Norm”, by Joe Raasch at The Happy Burro
Getting the most out of each moment in life.

“… my mentors”, by Karin H. at The Kiss Business Too
Two mentors, and what Karin H. learned from them.

“… Procrastinating”, by Yvonne Russell at Grow Your Writing Business
This piece will inspire you to procrastinate more often, if you could be bothered. Perhaps later on.

“… a Squirrel”, by G.L. Hoffman at What Would Dad Say
A nice story, with a zinger at the end.

“… Blogging”, by Gayla McCord at Mom Gadget
Someone who makes money by writing 12 blogs talks about that, and why she doesn’t go insane writing so much. Very informative.

“… a Weight Problem”, by Monique Attinger at Insurance Guide 101
Learn about overcoming a weight problem, and also about insurance. What more could you want?

“… Taking Out the Garbage”, by Michael Chantrel at Mortgage Guide 101 Blog
Why taking out the garbage is just like paying off a mortgage.

“… RUMMAGING!” by William Tully at LOGICal eMOTIONs
Looking at a product at the end of its life cycle, and winding the clock back.

“… A Light Switch”, by Robert Hruzek at Middle Zone Musings
When Cooking Goes Bad, or why you learn most about someone by viewing them when things go wrong.

Posted in Blogging, Deep & Meaningful | 3 Comments

1984 arrives in Britain in 2007

1984-movie-bb_a1.jpg

According to Spiked Online, those CCTVs that have become so ubiqitous in the UK have now been rigged up with loudspeakers, allowing remote faceless government agents to tell you not to loiter, or litter, or whetever.

But who will be doing the voices? Children, that’s who. The government has issued a press release stating the following:

‘Children from across the country will be very publicly calling upon the small minority of people who think it is acceptable to act anti-socially on our streets and in our towns to change their ways and take responsibility for their actions….’

Is the British Government trying to emulate 1984? Let’s take a look:

  • Loads of cameras following your every move? Check.
  • Cameras that can talk and give orders? Check.
  • The transformation of children into government spies? Check.
  • Government surveillance inside private homes? Not yet, in the UK anyway.
  • Use of torture to break “thought criminals”? No, thank God.
  • Anti-Sex League? Erm, no.

In short, the British Government is emulating 1984 as much as it is possible for a democratic government to do – so let’s hope it goes no further. If we ever hear Tony Blair say “We’re at war on terror. We’ve always been at war on terror.” than you will know they have crossed the line.

(If you haven’t read 1984, read it online – and shudder.)

Posted in Communism, Evil, Rants, Tech | 2 Comments