My swift code below is rotate a object at a 45 degree angle. My code below is having no effect on the rotation. I know I could rotate it i just create a frame but I need the constraints. I am not sure if this is possible but I would image it could be. All my code is below no storyboard needed. I tried to rate it on the box.transform line.
import UIKit
class ViewController: UIViewController {
var box = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
box.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(box)
box.backgroundColor = .red
box.transform = box.transform.rotated(by: .pi)
NSLayoutConstraint.activate([
box.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.25),
box.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5),
box.centerXAnchor.constraint(equalTo: view.centerXAnchor),
box.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}
Here rotating box
by pi
gives the same output because it's getting rotated 180 degrees. And box
looks the same upside-down. If you're trying to rotate box
by 45 degrees, then
Replace this:
box.transform = box.transform.rotated(by: .pi)
With this:
box.transform = box.transform.rotated(by: .pi/4)