问题
I am using scale affine transformations in Swift and have noticed CGAffineTransformMakeScale
does not work the same on all iOS versions. To demonstrate the differences, I have created a new Xcode 7 project, set up three test boxes on the Xcode Storyboard running on an iOS7 device, iOS8 simulator and iOS9 simulator.
Box A - has no constraints applied and is positioned centre top on the Storyboard
Box B - has height and width set along with center horizontal and center vertical alignment constraints.
Box C - has height and width set along with bottom space and center horizontal alignment constraints.
Boxes are then scaled to 0.5 using the below code.
Note: the pink areas aren't boxes or containers, but are used to highlight the position of the blue boxes after a scale affine transformation has occurred.
Results:
iOS7 there are problems- while all boxes halve their size, two boxes, A and C, don’t remain centred in place.
iOS8/iOS9 works as expected- all boxes halve their size and remain centered in place whether or not constraints are applied.
Questions:
What is causing this problem and how can it be best corrected and solved so that all iOS7/8/9 versions work identically?
CGAffineTransformMakeScale
on iOS7 - does not scale as expected :-(
CGAffineTransformMakeScale
on iOS8/iOS9 - scales as expected :-)
Code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var ButtonA: UIButton!
@IBOutlet weak var ButtonB: UIButton!
@IBOutlet weak var ButtonC: UIButton!
@IBAction func ButtonScale(sender: AnyObject) {
self.ButtonA.transform = CGAffineTransformMakeScale(0.5, 0.5)
self.ButtonB.transform = CGAffineTransformMakeScale(0.5, 0.5)
self.ButtonC.transform = CGAffineTransformMakeScale(0.5, 0.5)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
回答1:
This is described by Constraints & transformations - How Auto Layout quietly became transform-friendly in iOS 8.
Basically, in iOS7 and older you should not set constraints on values that are on different sides of a transformation.
来源:https://stackoverflow.com/questions/35207177/scale-affine-transformation-scaling-differently-on-ios7-and-ios8-ios9-in-swift