twotoasters/TTSwitch

Auto Layout

Closed this issue · 7 comments

It doesn't appear to work with auto layout.

In the storyboard if the Auto Layout is turned on the switch cannot be dragged or moved. It only can be clicked.

On the other hand, when auto layout is on, if the switch value is on, the thumb and track are in the wrong place.

I will write up a test for this and try to get this fixed this week.

Many thanks, thanks for your time.

I am not sure why this won't work. When I turn auto layout on the xib in the example project it will not even detect touches. A work around would be to place it in its own xib with auto layout turned off. I will keep looking in to this...

From just glancing at this, it appears that the xib doesn't have enough size/layout information about the switch when being layed out in the xib file. Just switching autolayout on the file on will give what looks like an over constrained layout in the console. This makes sense, as the nib file (not wanting to build an ambigious layout), will give the switch more constraints than it really needs. I "fixed" this by assuming that the switch is going to be a constant size (76, 28) and using this information to "turn off" the extranneous constraints by giving them a lower priority:
Screen Shot 2013-02-02 at 4 59 26 PM

To support autolayout, I added an intrinsicContentSize to TTSwitch so that autolayout could generate some initial constraints based on the size that we know it's going to be:

- (CGSize)intrinsicContentSize{
    return CGSizeMake(76.f, 28.f);
}

All of this gave me a switch in my layout without an ambiguous layout, without throwing any layout exceptions, and accepting all gesture recognizers. I can't fully explain why this works, but it has to do with how the nib automatically generates constraints to avoid an ambiguous layout. Hope this info helps.

@larsacus this is great 👍! I will get a pull of this together. Thanks!