IOS8 differently: Autolayout with view.transform = CGAffineTransformMakeScale (0.5, 0.5), the view is not set correctly

I use a machine

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

There is another view above this switch. This master switch must be aligned with this leading edge of the view, and there is a fixed vertical space between the view and the switch, for example, the following image:

enter image description here IOS7 Views

However, on iOS8 there is a different look:

enter image description here IOS8 Views

It appears that the restrictions do not apply to this switch.

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

This does not work.

How to enable this switch in the lower right corner of the above view?

Here the switch does not scale

enter image description here

+6
source share
2 answers

You have two possible ways to go here: either a subclass of UISwitch, or try to apply all the necessary changes to the subclass or define a new UIView that includes a modified UISwitch as a subview. A subclass may be the fastest, but it is more complicated and risky, since UISwitch was not intended for subclasses for such modifications.

Suppose you send a subclass route, here is what you need to do:

  • apply the scaling transform to self , obviously.

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

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

Basically, number 1 should do the actual visual conversion. But 2 and 3 are necessary in order to inform Auto Layout about the size of the new view, since by default AL is not aware of the transformations.

0
source

The view transforms around the view center, so when you call

 view.transform = CGAffineTransformMakeScale(0.5, 0.5) 

It makes sense that this is similar to iOS 8, the reason it looks good in iOS 7 is probably due to a bug in iOS 7.

Try changing the leading constraint constant as follows:

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

Thus, you can compensate in the offside that you have in iOS 8.

0
source

Source: https://habr.com/ru/post/975464/


All Articles