person101
person101

Reputation: 53

How to rotate a UILabel upside down in XCode, using Swift?

I would like to have a label that is displayed upside down, when the view did load.

Here is the default code given in the ViewController.swift, and I had also added the UILabel as an outlet.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var Label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Also sorry if this is a dumb question I am very new to IOS development and swift.

Upvotes: 4

Views: 3136

Answers (2)

Abdul Karim
Abdul Karim

Reputation: 4523

Swift 3x, Xcode 8x

override func viewDidLoad() {
    super.viewDidLoad()
    var aLabel: UILabel = ...
    aLabel.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))
}

Xcode 8.3.3

override func viewDidLoad() {
    super.viewDidLoad()
    var aLabel: UILabel = ...
    aLabel.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
}

Upvotes: 3

AnthoPak
AnthoPak

Reputation: 4391

To rotate your label in viewDidLoad just use the following :

Swift

override func viewDidLoad() {
    super.viewDidLoad()
    var aLabel: UILabel = ...
    aLabel.transform = CGAffineTransformMakeRotation(M_PI)
}

Objective C

-(void) viewDidLoad {
    [super viewDidLoad];

    UILabel *aLabel = ...
    aLabel.transform = CGAffineTransformMakeRotation(M_PI);
}

M_PI will make a rotation of 180 degrees which will result in an upside down label.

Upvotes: 10

Related Questions