Luka Filipovic
Luka Filipovic

Reputation: 51

How to change position of label in swift

i had read a lot how to change position of label but i cant do it. Here is my simple code

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var TEKST: UILabel!
    @IBOutlet weak var klik: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.green
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func kliksad(_ sender: UIButton) {
        if (view.backgroundColor==UIColor.green){
            TEKST.isHidden = false
            TEKST.text = "Hi"
            view.backgroundColor = UIColor.red
            TEKST.frame.size.width = view.frame.size.width
        } else if(view.backgroundColor == UIColor.red) {
            view.backgroundColor=UIColor.green
            TEKST.textColor=UIColor.purple
            TEKST.text = " Hello "
            TEKST.frame.origin = CGPoint(x: 10, y: 50)
            print(TEKST.frame)
        }
    }
}

Upvotes: 5

Views: 12810

Answers (1)

iOS Geek
iOS Geek

Reputation: 4855

Check for me its working

1) Label with constraints

enter image description here

enter image description here

2) My code

labelToMove.frame.origin = CGPoint(x: 20, y: 20)

[![enter image description here][3]][3]

3) result

enter image description here

Secondly this is your Button Action Code

if (view.backgroundColor==UIColor.green){
            TEKST.isHidden = false
            TEKST.text = "Hi"
            view.backgroundColor = UIColor.red
            TEKST.frame.size.width = view.frame.size.width
        } else if(view.backgroundColor == UIColor.red) {
            view.backgroundColor=UIColor.green
            TEKST.textColor=UIColor.purple
            TEKST.text = " Hello "
            TEKST.frame.origin = CGPoint(x: 10, y: 50)
            print(TEKST.frame)
        }

You had initialised View.color as green In didLoad then in action you are checking if color is green make it to red and using else if statement , second statement won't be executed here

   @IBAction func MOveLabell(_ sender: Any) {
        if (view.backgroundColor==UIColor.green){
            view.backgroundColor = UIColor.red
        } 

        if(view.backgroundColor == UIColor.red) {
            view.backgroundColor=UIColor.red
            labelToMove.frame.origin = CGPoint(x: 20, y: 20)
        }

    }

Upvotes: 4

Related Questions