7.2 C
Canberra
Thursday, October 23, 2025

Uniquely figuring out views – The.Swift.Dev.



ยท 1 min learn


Discover ways to use string primarily based UIView identifiers as a substitute of tags. If you’re bored with tagging views, try these different options.

First strategy: accessibility to the rescue!

Lengthy story brief, I used to be fairly bored with tagging views with silly quantity values, so I regarded for a greater different answer to repair my drawback. Because it turned out, there’s a property known 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 approach. As a free of charge storing the identify underneath the accessibilityIdentifier will profit your UI checks. ๐Ÿ˜‰

Second strategy: utilizing enums

The principle thought is to have an Int primarily based enum for each view identifier, so principally you need to use the tag property to retailer the enumโ€™s rawValue. Itโ€™s nonetheless not so good because the one above, however itโ€™s far more protected 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 strategy proper after I copy & pasted the primary snippet to this text, however what the heck, perhaps another person will prefer it. ๐Ÿ˜‚

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

Associated posts


On this article I’ve gathered my prime 10 favourite fashionable UIKit suggestions that I would undoubtedly need to know earlier than I begin my subsequent venture.


Discover ways to construct complicated types with my up to date assortment view view-model framework with out the battle utilizing Swift.


Do you need to learn 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 kind constructing with assortment views sucks.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles