WORKGROUND
Create a UIView with UILabel and UIButton. Here i have set layer properties of UIButton below:
[self.btnDropdown.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[self.btnDropdown.layer setBorderWidth:0.8f];
[self.btnDropdown.layer setCornerRadius:4.0f];
Now i am saving this custom view into file using NSCoding and load the same. This control is displayed in UITableViewCell.
ISSUE:
All the details of custom view like UILable title, size, background color etc. Same for the UIButton are retrieved from file. But CALayer related details which i have set for UIButton are not retrieved.
Before saving to file:
After getting from file:
So I am not clear about that CALayer properties are manage with UIButton object.
Is these details (CALAyer values) are visible only at run time? Or i am missing something?
Please add a comment if question is unclear to you.
EDIT:
Previously i have not provide more information to reduce the complexity of the question. Below is complete hierarchy of my implementation.
NSObject --> Custom View --> UIButton --> CALayer
1) First NSObject class is a bean class. Which contains some variables (i called it Property). This properties are meta details of Custom view. There is one variable "parentView" is use to store Custom view.
2) Custom view: This is UIView class which contains UILabel and UIButton
Now bean class object (Property) is stored in file and retried. So that i can use variable parentView to load Custom view.
And as per question about the UIButton is embedded in that Custom view.
Figure it out:
Here button shown in images custom views. I used to save these control in local database with BLOB.
Thing is that these views object/instance are stored in database with some properties.
Now when i retrieve them and add them to any view as a subview their -init method is not getting called. (Any how i figure that out) and called -init method for these views.
Related
I'm targeting IOS 8+.
I have a form that is used in more than one place. So I decided to create a custom view where I define the various "form" text fields.
I have built my XIB, and the UIView subclass contains the outlets for each textField.
The view is composed of a background image and a scroll with the form fields over it.
Now, my first obstacle was: I need to have this custom view in a container that may or may not have a navigation bar. This made me create a constraint outlet so I could update its value to push down the scroller view. This way I'd have the whole image in the frame, the top being behind the navbar and the scroller bellow the nav bar).
Here's a manual drawing to help understanding the problem.
It's very possible that I'm making a lot of mess and confusion on my way to solve this. :)
The problem is:
After awakeFromNib runs I have no access to the constraint property. I then noticed the same thing happens for the TextFields outlets.
So, how can I access the custom view's properties when I instantiate them programatically?
Something like:
Controller:
let customView = SignupView(frame: f)
view.addSubview(customView)
customView.pushScrollerDownBy(50.0)
Custom view:
func pushScrollerDownBy(yOffset: CGFloat) {
//topScrollerConstraint is the outlet for the textField.
topScrollerConstraint.constant = yOffset //right now topScrollerConstraint is nil.
}
You should check if you have connected your topScrollerConstraint to the file's owner since it will not get instantiated and therefore, error. Here is a recent SO question regarding difference between these two:
What is File’s owner in XIB in this case?
I am tired of setting property values to each UILabel in the IB. Is extending the UILabel the only possible way? or there exists some other way to do it?
You could create an IBOuletCollection for the labels and set the properties programmatically (in viewDidLoad for example) using:
[self.outletCollection setValue:whatever forKey:something];
You can also call makeObjectsPerformSelector on the collection in order to send messages to each label.
Assign all the properties to one UILabel
Copy paste the label as many times you want in storyboard or xib
New labels will also get the same properties, you just require to arrange their positions
Then you can go ahead with IBOutlets and manipulate with those
This applies to all the widgets in storyboard/xib.
Also as #dehlen metioned,
If you are using codebase for UI then, Custom Button Class
Inherit UIButton and create a subclass CustomButton
In its init method, assign all the properties which you require, like font, color etc
And assign frame value at use-time, through initWithFrame method.
Or
Add button in storyboard/xib, but in its class property change class name to CustomButton
Thanks #dehlen, You are right, I forgot to mention code side implementation.
After setting the property of a UILabel make copy of it by
Press and hold alt and drag and drop UILabel to another place it will create new copy of that UILabel with same properties.
I have a class MyButton : UIButton which inherits from UIButton. I do a bunch of things in the initWithFrame (the only constructor)of MyButton say like setting the backgroundcolor.
Now I want to use this MyButton in my xib. so that I dont keep setting these properties again and again for all my buttons. I have also set the Custom Class to MyButton in the Identity Inspector for the button in the xib.
Nothing still reflects the properties I set in the xib. This could have been easily done if it was in the code.
My question is,
1) What gets called when you create button thru a xib (like you call initWithFrame when you programmatically create a button) ?
2) How do I get it to see the properties I set in the MyButton ? Is moving out of xib and doing it programatically the only way ?
thanks in advance !
Typically with initWithCoder:
You can use the identity inspector in Interface Builder to set values using the keypath of the attributes. In this example, you can change the CALayer properties of the view:
You can simply set it in your xib file. For that:
Select the button in the xib file.
Click the Identity Inspector button.
Type the name of your custom class(MyButton for your case) in the Class field.
Now your button will be with the Type MyButton class.
EDIT
Check Apple's documentation on Adding a Custom Object. This will give you more idea.
I'm trying to get a handle on what's happening in my app...
I have a UICollectionViewCell subclass which has a UIImageView as a property.
In that subclass's init method, I programatically add the imageView property as a subView of the classes contentView:
[self.contentView addSubview:self.imageView];
At the point this happens, the 'self.imageView' property is null as the imageView has not been created and assigned to the property yet.
Later in my cellForItemAtIndexPath method, I assign the imageView property of the cell equal to an imageView object.
At this point I expect that the content view would now have a valid imageView subclass, but that's not what i'm seeing.
So my question is: Why doesn't it work?
Should I be able to add my 'null' property imageView as a subView of my self.contentView and then later assign the imageView property equal to an imageView object, and then see that object as one of the contentViews subviews?
I hope this question makes sense, it was a pain to describe!
Adding a subview which is nil sounds wrong to me
I think you are using a wrong approach. You should instantiate and add your imageView in your cell´s init method and then, in the cellForItemAtIndexPath of your controller assign the image like this
cell.imageView.image = the image you want...
No, you shouldn't (and you see it doesn't work). A property is a reference to an instance of something. If that instance doesn't exist yet then you can't work with it. Trying to isn't setting up some rule for future use, it's just quietly doing nothing.
You can add a method like configureImageView: and call that instead. The implementation would set the property and add the image view as a subview.
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