Can't Programmatically Change Color Set in Storyboard as Color from Xcassets Catalog

Can't programmatically change color set in storyboard as color from xcassets catalog

When a UIView subClass like UITableViewCell is loaded from the Storyboard/Xib, it applies the attributes specified in Attribute Inspector to all the subViews. We have the following callback methods to know when a view is loaded from the Storyboard/Xib,

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
}

override func awakeFromNib() {
super.awakeFromNib()
}

These methods could be good candidates to add/remove a subView but they are not supposed to update the subView's size or some of attribute inspector related properties. The recommended method to update subViews is when the super view finishes loading and applying all the attribute inspector properties and calls layoutSubviews. So then you should apply any cosmetic change to a subView. e.g,

override func layoutSubviews() {
super.layoutSubviews()

label.textColor = UIColor(named: "HighlightedGreen")
}

For a UITableViewCell, any object implementing UITableViewDataSource also guarantees a delegate method to apply any cosmetic change before the cell is being displayed as below, So this is also another good candidate to change the color.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! MyListTableViewCell).label.textColor = UIColor(named: "HighlightedGreen")
}

Image asset catalog color not changing

Issue 1) That seems issue with Xcode, next releases may come up with fix.

Issue 2) That seems issue with Xcode too.. When I faced that situation I had to add ImageView programmatically which worked. You may also try IBDesignable or Custom class or Extension which changes renderMode property.

Add a custom color palette to Xcode Interface Builder

Yes, you can create and edit a palette in IB and then share it with your team This article has all the details: http://natashatherobot.com/xcode-color-palette/

Edit:
That link has expired. You can see the web archive here: https://web.archive.org/web/20210303015055/http://natashatherobot.com/xcode-color-palette/

But, using named colors in the asset catalog is a better way to go now.

How to change dynamically color used in UIStoryboard

I finally found the solution.. (had to wait for the tool to exist :p).

Crayons is an Alcatraz allowing you to re-use some code define color in Storyboard! yeah!

check it out: https://github.com/Sephiroth87/Crayons

iOS UILabel textColor not updating in dark mode

Somehow the label in collection view is not working as expected. I tried different configurations and none worked. I ended up using button instead and it worked in my case. I will update my answer once I get this working with a label.



Related Topics



Leave a reply



Submit