person101 person101 - 4 months ago 11
Swift Question

How to make 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.

Answer

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.

Comments