I am using storyboard and there are 5 view controllers in it. Each ViewController has only 1 UIButton. I want to set button's background color using User Defined Runtime Attributes. So I defined a keyPath "bgColor" and set corresponding hex color code "#ffaa11". But application crashes before loading the view. It gives exception:
[<UIRoundedRectButton 0x713fdb0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bgColor.
I dont want to create custom class. Please suggest how can I paas value through IB. Please check the snapshot.
NOTE: In real my requirement is to get a string value which has been entered in Interface Builder. Very similar to User Defined Runtime attributes. I have taken example of bg color.
Why are you using User Defined Runtime Attributes anyway? Switch to the Attributes Inspector and choose button's background from there.
First thing first.
Obviously it crashes. Because there is no bgColor property on the button. Hence when you are trying to set it as button.bgColor = #yourValue. It is crashing.
If you are using ios7 try setting the button's tintColor property. and for styles go through tintAdjustmentMode values.
And after these . If it still doesn't solve your issue. We will discuss something else :) :)
cheers. Have fun.
Related
After searching online I found that, if we declare a property inside a custom view as #IBInspectable, that property can be altered in the IB attribute inspector.
But my question is what is the use of #IBDesignable. Even if I don't declare a class as # IBDesignable, Im able to see the live rendering of the #IBInspectable properties
IBDesignable is user defined run time. Mean that if any IBDesignable view is rendered on the storyboard, you will find the view and change the value of the IBInspectable property, this will directly reflect on the storyboard without running the app.
IBInspectable is run time property, this works as key coding value. You can't find the change in storyboard, it will be applied in the run time.
The live rendering of a custom view should stop once you remove #IBDesignable. But sometimes this does not happen immediately.
If you remove #IBDesignable, then, close and reopen Xcode.
You should not see any live rendering the custom view directly in the canvas.
Say you are setting
field
.defaultTextAttributes
.updateValue(20.0, forKey: NSAttributedStringKey.kern.rawValue)
(BTW you do that to space out text, like t h i s.)
in fact, is there a way to set that using User Defined Runtime Attributes,
right on the storyboard in Xcode?
No, there is not. User Defined Runtime Attributes works only where key-value coding would work, with a limited range of value types. A moment's thought will reveal that your code can't be expressed in that way.
You could, however, subclass to define a custom property and set it to 20 in User Defined Runtime Attributes, and respond with a setter observer to run the code you've shown. That way, different fields can have different kern values depending on a setting in the storyboard.
(If you're going to do that, you might as well make this property IBInspectable; IBInspectable effectively is User Defined Runtime Attributes, with a different interface.)
I've recently started developing an iOS app, which I've never done before, so it's been going a bit slow, but I'm learning, so that's understandable.
I want to make a custom interface, so I've been making subclasses of the default view classes (like UIButton) so that I can define custom drawing. I've been told this is the best way to define custom interface elements that can be reusable. It definitely seems to be working that way. However, I haven't been able to make elements completely reusable by just using a subclass.
For example, in order to prevent a button's text from changing color when it is clicked, I have to manually go into the interface builder and set the button type to "Custom." After that, code that I enter into the subclass's constructor to change attributes seems to work. But I have to do this for every button I add, and in code the "buttonType" attribute is read only. Is there a way for me to define (just once) certain attributes for every instance of my button subclass that I add to the interface?
My goal is to be able to have a button subclass or template that defines all attribute values that I want my buttons to have, and every instance that I add automatically reflects those properties without me having to change anything. More so, I want to be able to modify that subclass/template and have those changes reflected in every existing instance. I have to imagine that this is possible in iOS. There is simply no way to build sophisticated interfaces without this capability.
Define a custom Button class (inherited from UIButton) in your project and in the init set the properties which you wanted to be set across.
In the interface builder go to the the class inspector and enter the button to be of the previously declared button.
buttonType needs to be set for all the button as this is defined at initialization time and exposed as read only property. If you want absolute reusability for your case, create a view, with an embedded button in code. when you create a button, create using the static method buttonWithType.
Wherever you need, drag and drop a UIView and set the view type to be the custom view.
I have an iOS app with multiple ViewControllers. Each view has numerous IB generated UIButtons, each set to custom. I need to change the color of the background in normal and highlighted states. Further, I need to vary the colors to an RGB value based on user interaction. Thus, I can't use image files.
I found an example of a custom class derived from UIButton that implements the color change and click methods to change the colors as I desired. I created a test button and changed its IB custom class to my new class.
I have an outlet property for my IB created button.
The problem I am having is in the viewcontroller.m file when I attempt to access the custom method in my class, xcode can't see the methods.
Use IBAction as a return type for that method
Declare that particular method in .h file
Apple's UIAccesibility Protocol reference states:
UIAccessibilityLabel
Discussion
The default value for this property is nil unless the receiver is a UIKit control, in which case the value is a label derived from the control’s title.
Well... both UIView and UIViewController are in the UIKit framework, but I can't find the default accessibility label for my view who's controlled by the UIViewController named LoginVC. I tried to log it, but get a blank string. Is that right? is the doc wrong? Or am I doing something wrong?
I know I can set the accessibility label manually; I'd like to avoid the clutter and use defaults if possible - which are already generally descriptive if their name is actually derived from their controller.
By default, a UIView and a UIViewController have nothing set for their title property, so the default accessibilityLabel property (which is derived from the title property) correctly and expectedly returns nil. Set their title properties (either in code or in Interface Builder) and you'll see it when you log accessibilityLabel at runtime.
I don't think Accessibility will populate itself automatically, if that's what you're asking.
If you asking for ways to set it, you should be able to do it programmatically:
[myButton setAccessibilityLabel:#"Hello"];
If you use Interface Builder, there is a field in the Identity Inspector tab that will allow you to set this. Select your view under Objects on the left. Then click the third tab from the left for Identity Inspector. Example below: