Use a constant in a storyboard?

Many locations in the storyboard using the same values, i.e. for x coordinates of representations. Can I control these values ​​by setting one constant from the code? Can I define a value in .h constants and refer to it in the Storyboard?

+4
source share
1 answer

Yes there is. Here is what I did:

I created a file with a name Labelsthat had a structure withCGFloats

import Foundation
import UIKit


struct Labels {

    let labelXCord:CGFloat = 21
    let labelYCord:CGFloat = 50

}

Then, in mine, ViewControllerI made struct a constant, and then used the constant from the Labels structure in label coordinates.

import UIKit

class ViewController: UIViewController {

    let labels = Labels()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let label = UILabel(frame: CGRectMake(labels.labelXCord, 0, 200, 21))
        label.center = CGPointMake(160, 284)
        label.textAlignment = NSTextAlignment.Center
        label.text = "I'am a test label"
        self.view.addSubview(label)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Update:

Is that more than what you were thinking?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label0: UILabel!
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!


    let labels = Labels()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        label0.frame = CGRectMake(labels.labelXCord, labels.labelYCord, 152, 152)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

And I think you can use this to change only the X coordinate:

label1.frame.origin.x = labels.labelXCord

for .

0

Source: https://habr.com/ru/post/1611547/


All Articles