Uniquely figuring out views – The.Swift.Dev.



ยท 1 min learn


Learn to use string based mostly UIView identifiers as an alternative of tags. In case you are bored with tagging views, take a look at these different options.

First method: accessibility to the rescue!

Lengthy story quick, I used to be fairly bored with tagging views with silly quantity values, so I regarded for a greater different answer to repair my downside. Because it turned out, there’s a property referred to as accessibilityIdentifier that may do the job.

extension UIView {

    var id: String? {
        get {
            return self.accessibilityIdentifier
        }
        set {
            self.accessibilityIdentifier = newValue
        }
    }

    func view(withId id: String) -> UIView? {
        if self.id == id {
            return self
        }
        for view in self.subviews {
            if let view = view.view(withId: id) {
                return view
            }
        }
        return nil
    }
}

I made a easy extension across the UIView class, so now I can use a correct string worth to uniquely establish any view object within the view hierarchy. Itโ€™s fairly a pleasant answer, now I can identify my views in a very nice manner. As a free of charge storing the identify below the accessibilityIdentifier will profit your UI exams. ๐Ÿ˜‰

Second method: utilizing enums

The principle thought is to have an Int based mostly enum for each view identifier, so principally you should utilize the tag property to retailer the enumโ€™s rawValue. Itโ€™s nonetheless not so good because the one above, nevertheless itโ€™s far more secure than counting on pure integers. ๐Ÿ˜ฌ

enum ViewIdentifier: Int {
    case submitButton
}

extension UIView {

    var identifier: ViewIdentifier? {
        set {
            if let worth = newValue {
                self.tag = worth.rawValue
            }
        }
        get {
            return ViewIdentifier(rawValue: self.tag)
        }
    }

    func view(withId id: ViewIdentifier) -> UIView? {
        return self.viewWithTag(id.rawValue)
    }
}

Truthfully I simply got here up with the second method proper after I copy & pasted the primary snippet to this text, however what the heck, possibly another person will prefer it. ๐Ÿ˜‚

In case you have a greater answer for this downside, be at liberty to share it with me.

Associated posts


On this article I’ve gathered my high 10 favourite trendy UIKit ideas that I might positively need to know earlier than I begin my subsequent challenge.


Learn to construct complicated kinds with my up to date assortment view view-model framework with out the battle utilizing Swift.


Do you need to discover ways to load a xib file to create a customized view object? Effectively, this UIKit tutorial is only for you written in Swift.


Just a bit recommendation about creating customized view programmatically and the reality about why type constructing with assortment views sucks.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles