I have prepared a custom UIView subclass with an associated Xib file. On the storyboard, I place a UIView and set it's class to my custom subclass. In the custom view's initWithCoder: method, I load the xib and initialize the subviews. This works great.
Now I want to use the same custom view elsewhere, but I would like the layout of my subviews to be different. I would like to make a second custom view layout in the same Xib file and load the correct one depending on which of my view controllers contains the custom view. Since all of my subviews and all of the logic are the same, just the layout is different, I'm looking for something like this:
-(id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
if (self.subviews.count == 0) {
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
UIView *subview;
if ([/*instantiating VC isKindOfClass:viewController1.class]*/) {
subview = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
}
else if ([/*instantiating VC isKindOfClass:viewController2.class]*/) {
subview = [[nib instantiateWithOwner:self options:nil] objectAtIndex:1];
}
subview.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview: subview];
}
}
return self;
}
Is there any way to access information about the view controller that is instantiating this custom view?
Yes there is a way, place two views and set the two view's tag different, say 10 and 20 in story board whose custom class you want to set with your UIView subclass.
Then in your UIView subclass do this:
-(id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
if (self.subviews.count == 0) {
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
UIView *subview;
if (self.tag == 10) {
subview = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
}
else if (self.tag == 20) {
subview = [[nib instantiateWithOwner:self options:nil] objectAtIndex:1];
}
subview.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview: subview];
}
}
return self; }
the tag 10 view in storyboard will be replaced by your first view and the tag 20 view in storyboard will be replaced by your second view.
Build, Run and Enjoy!!!
Related
I have a xib with constraints, and I have a viewController in my autolayout storyboard with a scrollView where I added the xib.
My xib have constraints, but when I add the xib to the autolayout storyboard the outlets change position and size, it is losing his constraints...
I use this function to create the xib :
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
nibname=#"myViewXib";
[self addSubview:
[[[NSBundle mainBundle] loadNibNamed:#"myViewXib" owner:self options:nil] objectAtIndex:0]];
[self setNeedsLayout];
}
return self;
}
And this is the function to load the xib into my storyboard in the viewDidLoad:
MyViewXib *myView = [[MyViewXib alloc] initWithFrame:CGRectMake(self.scroll.frame.origin.x, self.scroll.frame.origin.y, self.scroll.frame.size.width, self.scroll.frame.size.height)];
[myView setFrame:CGRectMake(width*count++, 0, width, height)];
[self.scroll addSubview:myView];
[self.scroll layoutSubviews];
What is wrong in my code? Why are the constraints not working?
thanks!
Instead of putting add subview code in the viewDidLoad method you can try it by putting it in viewDidAppear method since the view controller don't have the exact frame of any view in viewDidLoad method.
I have a tableview with 2 rows. In the first row I have an UIView and I'm gonna add a custom view on it. I made the custom view as a XIB and I'm adding it like following,
Layout6View *lookLayout6 = [[Layout6View alloc] initWithFrame:CGRectMake(0, 0,cell.mainView.frame.size.width, cell.mainView.frame.size.height)];
[cell.mainView addSubview:lookLayout6];
and my Layout6View looks like,
#implementation KCTLLookLayout6View
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self = [[[NSBundle mainBundle] loadNibNamed:#"Layout6"
owner:self
options:nil] lastObject];
self.frame = frame;
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
for (UIView *view in self.subviews) {
if (view == self.imageTopRightCorner) {
view.frame = CGRectOffset(CGRectMake(0, 0, /*(self.frame.size.width/2)-20, (self.frame.size.height/3)*/30,30), 0, 0);
}
}
}
#end
When I'm running this, the custom view didn't resized. It's spread all over the table view(not only inside the cell).
Is there a way to fix this?
Try:
[cell.contentView addSubview:lookLayout6];
Change the Size property of xib view. By default it is Inferred, make it Freeform and check.
I am having huge problems with learning autolayout especially when integrating xibs and uiscrollviews. In an effort to simplify my problem, I started a new project with a single view tied to a storyboard. I then subclassed UIView (Diptic) and created a xib file for it. My storyboard is not using autolayout, but my Diptic xib is. Right now I want to have a horizontal scrollView with a few Diptic instances layed out across it. But I am only ever getting the first diptic instance to show up, because the frame isn't being initialized correctly.
In my ViewController.m's viewDidLoad:
self.scrollView.contentSize = CGSizeMake((self.view.frame.size.width+10)*5, self.view.frame.size.height);
for (int i = 0; i < 5; i++){
int x = i*(self.view.frame.size.width+10);
Diptic *diptic = [[Diptic alloc] initWithFrame:CGRectMake(x, 50, self.view.frame.size.width, self.view.frame.size.height-100)];
[self.scrollView addSubview:diptic];
[array addObject:diptic];
UIView *greenBox = [[UIView alloc] initWithFrame:CGRectMake(x, 5, 40, 40)];
greenBox.backgroundColor = [UIColor greenColor];
[self.scrollView addSubview:greenBox];
}
Diptic.m
#import "Diptic.h"
#implementation Diptic
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"Diptic" owner:nil options:nil];
self = [views objectAtIndex:0];
self.label.text = #"WOOT";
self.backgroundColor = [UIColor redColor];
}
return self;
}
#end
If I set the frame after I add it to the view it seems to work, but why can't I set the frame with initWithFrame?
The problem here is that you assign the frame to a view that is created with a call to [super initWithFrame:frame]. When the NIB is loaded in - (id)initWithFrame:(CGRect)frame, self is replaced with a view from [views objectAtIndex:0] and the frame for this view is never set. Instead, eliminate the call to super and load the view from the NIB instead:
#import "Diptic.h"
#implementation Diptic
- (id)initWithFrame:(CGRect)frame
{
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"Diptic" owner:nil options:nil];
self = [views objectAtIndex:0];
if (self) {
self.frame = frame;
self.label.text = #"WOOT";
self.backgroundColor = [UIColor redColor];
}
return self;
}
#end
When init a view/controller from storyboard or from xib, the init method is
- (id)initWithCoder:(NSCoder *)decoder
try to do other things in this method
I created a UIView subclass associated with .xib file. This UIView subclass is to be used in a UIViewController. In the controller, I think there are two options how we instantiate the UIView subclass:
MyUIView *myView=[[MyUIView alloc] initWithFrame:aRect];
and
MyUIView *myView = [[[NSBundle mainBundle] loadNibNamed:#"MyUIView"
owner:self
options:nil] lastObject];
I prefer the first approach or its variants that allow me to perform custom initialization. The only problem is that I have to specify a frame's rect, which was already specified in .xib (I mean frame's height and width of MyUIView). Yes, I can hardcode it again in aRect, but this is tedious to maintain (e.g., when I change position of UIs in .xib, I have to update aRect, too).
So the second approach should come into mind as the frame rect is automatically set. The remaining problem is I cannot customize the initializer (say, I want to pass extra parameters during initialization).
What's your preference? Which one is better in your opinion?
EDIT1:
Inspired by sergio's answer, I came out with this workaround:
// In MyViewController.m
MyUIView *myView=[[MyUIView alloc] initWithFrame:CGRectMake(x, y, 0.0, 0.0)];
// In MyView.m
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self = [[[NSBundle mainBundle] loadNibNamed:#"UnmovableTagView"
owner:self
options:nil] lastObject];
[self setFrame:CGRectMake(frame.origin.x,
frame.origin.y,
[self frame].size.width,
[self frame].size.height)];
// frame's width and height already determined after
// loadNibNamed was called
...
}
return self;
}
Have you tried using:
MyUIView *myView=[[MyUIView alloc] initWithFrame:CGRectZero];
I don't know if it works in your case (loading the view from a nib), but I use it successfully when I create my custom view programmatically.
You can give it a try.
EDIT:
you could define your own init method for your view:
-(id)initWithPosition:(CGPoint)pos;
and then call:
-(id)initWithPosition:(CGPoint)pos {
if (self = [super initWithFrame:{pos,{0,0}}]) {
self = [[[NSBundle mainBundle] loadNibNamed:#"UnmovableTagView"
owner:self
options:nil] lastObject];
[self setFrame:CGRectMake(frame.origin.x,
frame.origin.y,
[self frame].size.width,
[self frame].size.height)];
// frame's width and height already determined
// after loadNibNamed was called
}
I want to position a view (red box with label and button) prepared in IB as Subview on the screen programmatically. The positioning works fine with the "blue lines" (there might actually be a better way to simply draw a line on a view?!). But the view of the "AttributeApertureView" view object sticks to the top instead of following the initFrame Parameter starting at y=60 as you can see in the screenshot.
//Add blue Line
CGRect lineB = CGRectMake(0, 48, bounds.size.width, 0.5);
UIView *secondLine = [[UIView alloc]initWithFrame:lineB];
[secondLine setBackgroundColor:[UIColor colorWithRed:0.396 green:0.796 blue:0.894 alpha:1]];
[[self view]addSubview:secondLine];
//Add Attribute Aperture
AttributeApertureView *aperture = [[AttributeApertureView alloc]initWithFrame:CGRectMake(0, 60, bounds.size.width, 50)];
[aperture setBackgroundColor:[UIColor redColor]];
[[self view]addSubview:aperture];
Init function in AttributeApertureView class. This should basically only load the related nib file with the IB Interface.
#implementation AttributeApertureView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"AttributeApertureView"
owner:self
options:nil];
self = [nib objectAtIndex:0];
}
return self;
}
What is the best way to position the "aperture" view on the screen?
Seems like calling loadNibNamed: resets the view's frame to what it is in the nib. This should work:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"AttributeApertureView"
owner:self
options:nil];
self = [nib objectAtIndex:0];
self.frame = frame;
}
return self;
}
because self = [super initWithFrame:frame]; sets the frame once, but self = [nib objectAtIndex:0]; ends up changing it again because you are resetting the whole view, so you have to set the frame again to be the one in the argument.