6

I am using auto layout,

I want to make a smaller UISwitch and then use view.transform = CGAffineTransformMakeScale(0.5, 0.5) to realize it.

There is another view above this switch. This switch's leading should be aligned to this view's leading edge, and there is a fixed vertical space between view and switch, like following picture:

enter image description here Views on iOS7

However, there is different appearance on iOS8:

enter image description here Views on iOS8

Seems like constraints are not applied to this switch.

But I called self.view.layoutSubviews() and self.view.layoutIfNeeded()

It doesn't work.

How to let this switch is always sticked to the right bottom corner of the above view?

Here is switch not scaled down

enter image description here

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Honghao Z
  • 1,419
  • 22
  • 29
  • Auto Layout doesn't work with layer transforms – Max MacLeod Sep 18 '14 at 07:46
  • @MaxMacLeod I just wondering why same code has different behaviour on iOS7 and iOS8. I think you mean this answer [link](http://stackoverflow.com/questions/12943107/how-do-i-adjust-the-anchor-point-of-a-calayer-when-auto-layout-is-being-used/14105757#14105757), right? – Honghao Z Sep 18 '14 at 14:45
  • I guess it behaves like that because the intrinsic content size of the UISwitch stays at the default w:51px h:31px despite the change of the view transform. You should try to subclass the UISwitch and override the - (CGSize)intrinsicContentSize method by returning CGSizeMake(25.5f, 15.5f) – Emmanuel Paris Sep 23 '14 at 17:15

2 Answers2

0

You have two possible ways to go here: either subclass UISwitch and try to apply all the needed modifications in the subclass, or define a new UIView which includes as a subview a modified UISwitch. The subclass might be the quickest, but it is trickier and riskier since UISwitch was not intended to be subclasses for this kind of modification.

Supposing you go the subclassing route, here are the modifications you need to make:

  1. apply the scaling transform to self, obviously.

  2. override alignmentRectForFrame:, so it returns super.alignmentRectForFrame: plus your scaling transform.

  3. override intrinsicContentSize, so it returns super.intrinsicContentSize plus your scaling transform

Basically, number 1 should perform the actual visual transformation. But 2 and 3 are necessary to inform Auto Layout about the size of the new view, since by default AL is not aware of the transforms.

algal
  • 27,584
  • 13
  • 78
  • 80
0

The transform of a view is taking place around the view's center, so when you call

view.transform = CGAffineTransformMakeScale(0.5, 0.5)

It makes sense that it looks like this in iOS 8, the reason that it looks good in iOS 7 is probably because of a bug in iOS 7.

Try to change the constant of the leading constraint like this:

self.switchLeadingConstraint.constant = -(self.mySwitch.bounds.size.width/4)

This way you can compensate on the offside that you have in iOS 8.

oren
  • 3,159
  • 18
  • 33