Speed up your PC today.
Today’s tutorial has been written to help you when you receive an error related to Interface Builder runtime custom attributes.
When building a user interface, thanks for an IDE that allows me to easily change visuals without having to worry about writing code. Writing simple things like colors, borders, fonts or shadows for marketers overwhelms my project. Luckily, I’m working with talented designers close to Atomic Object who can already use Xcode to make some of these aesthetic UI changes without having to write any additional code.Additional code.
However, nowadays designers can make very selective changes to certain controls, and even whatever they want to do with them is not possible in the IDE. For example, I can’t set a custom border or shadow for a UIView letter without code, or change the font of a UISegmentedControl. I wish Xcode interface builder could change such simple properties.
Fortunately, Xcode has a feature associated with Interface Builder that allows you to manually add custom rendering attributes. You tell it the username, the property you want to change, then the type of the property, and finally the importance.
The functionality of custom attributes at run time is limited to the type of properties that can be changed to Boolean, NSNumber, NSString, CGPoint, CGSize, CGRect and nsrange, UIColor. If the property you want to change is of a different type, you must write code to change the property.
This is a new problem if, for example, you normally set the border color. The type of the layer’s CGColor
borderColor property is not one of the valid types. The advantage of the object-c category allowsYou can’t get around this limitation and swap properties of different types.
An Objective-C Category That Makes This Possible
The first step I’ll show you is changing the border color and darkness of any UIView. The render attribute function allows us to define the UIColor type that we need to convert to the CGColor type in the market. To do this, we need to extend the
CALayer class with a field that can transform a UIColor if you need the CGColor we need for our border and shadow.
You can teach Objective-C to the whole family. I have added two property names
shadowIBColor which are of type UIColor. IB stands for interface when it comes to constructors. I need to give these person properties a unique name to avoid naming conflicts with the original locations named borderColor and shadowColor which are usually of type CGColor.
How to add user defined runtime attributes in Xcode?
Luckily, Xcode’s Interface Builder has a feature that allows you to manually add custom learning attributes. You tell it the tag of the property you want to change, then use the property to specify the type, and finally your current value.
shadowIBColor take a UIColor value and set the shadow and border color properties for the entire layer to match. The runtime value set by iOS is based on whatthen you set in the runtime attributes.
Example: User Interface
Here is a test UI I put together to test our awesome new runtime attributes. The gray rectangle is the correct UIView to which we will add each border and shadow. I’ll also show you some of the UISegmentedControl controls that we’ll customize later in this article.
Make sure the gray UIView is selected, and add the following runtime attributes:
What is @ibinspectable attribute in UIView?
This validated attribute can be applied to other UIView properties not normally found in Interface Builder. Let’s extend the borderColor property. Because our custom @IBInspectable attribute relies on the runtime custom attribute mechanism, we’ll stick with types that can now be set by Xcode’s interface constructor.
Note that we are using our new UILayer attributes
shadowIBColor. You can set them to the full color value you want. I used black for the shadow and red for the border. I have also set all
cornerRadius which gives you an example of the properties you then with using the Exact Runtime Attributes function.
Note that if you change the value of a property in runtime functions, you won’t see any UI fixes specifically at Interface Construction Company. Your view will still bet look like a solid gray rectangle. These settings are set at runtime, so we need to run the simulator when you see your changes at runtime.
Resize Segmented Control Font Family
Next, I’ll set the font for the nice UISegmentedControl. Can I change the hues with the runtime attribute but I can’t change the font size or only the font family. The title is a little trickier to set, as your family may have different text depending on the status you choose. We can use the same technique using styling to extend the UISegmented control and improve the font family and size that has attributes and runtime values. I am currently setting the header font from normal.
Now select the segmented controls and add all of the following runtime attributes. You can choose any color you want. If you are looking for a list of possible last names, you canYou can go to iosfonts.com
My example is a bit ugly, but someone got it right. Now you can change all these houses without changing the code. If you’d like to try the above code for yourself to see how it works in action, I have a detailed project on GitHub. Can you imagine that when using the silent method, some settings will be changed at runtime to get different controls?