I'm designing using IB a specific button (with some labels,and imageView and other stuff).
I would like then to use that xib object (the button) in another xib, where I have 6 objects of that button.
I know I can do that programmatically using the Class Identifier, but then I have to position my lables and image view, and set the default values and everything else in code.
I'm wondering if it's possible to do the same thing using just the IB ? (of course I would still set the values in code, but I want to positions of the lables/imageView and all the rest to be set in the xib)
Thanks
Edit
So, I understand what I asked for at first is not possible.
What I'm trying now is like that :
I created a ButtonTest.xib
.
Inside the Xib I have a UIView and 3 subviews (2 lables and a button).
In the inspector I set the UIView class to ButtonTest
.
Inside ButtonTest.m I have 3 outlets for each of the subviews, which I connect in IB.
Next I have a ButtonTestViewController.xib
.
Inside it I put one view and set it's class in the inspector to be ButtonTest
.
I connect that view to a myTextView
outlet inside ButtonTestViewController.m
of class ButtonTest
Now, this is the code inside ButtonTestViewController.m viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"ButtonTest" owner:nil options:nil];
ButtonTest *mainView = (ButtonTest*)[subviewArray objectAtIndex:0];
self.myTextView = mainView;
}
What I hoped would happen, is that the view in ButtonTestViewController.xib would become the view I designed in ButtonTest.xib.
This just isn't happening. what happens is that the view inside ButtonTestViewController.xib stays the same.
Worth mentioning, is that if I add:
[self.view addSubview:mainView];
It does add the new view, besides the existing one.
Any idea how to do what I want ?
Eventually I would like to have 6 views in ButtonTestViewController.xib
, all would look like the ButtonTest.xib template, and the lables values will be set in code.
Edit2
Ok, guys I did everything you said and it worked like a charm.
The only problem I have right now with this issue is when the view in ButtonTestViewController.xib
is a little bigger then view in ButtonTest.xib
.
When that happens, The lable on the button look extremely blurry.
When they are both the same size, it's all good.
@ Ned - I used the exact code you posted in my InitWithCoder method, except I switched the frame sentence to this :
self.bounds = mainView.frame;
I tried playing with the content mode in IB trying to fix it, to no avail.
When I do use it like you posted, meaning :
mainView.frame = self.bounds;
It's not working at all, and both views' sizes stay the same.
Here I tried playing with the resizeMask but still didn't work.
And idea how to fix these 2 issues ?
Thanks guys!
Edit 3
I did manage to fix one of the issues, resizing the ButtonTestViewController.xib
to the ButtonTest.xib
view size.
This is what I did (using code to solve the blurry issue, taken from here)
self.bounds = CGRectMake(0, 0, mainView.frame.size.width, mainView.frame.size.height);
CGRect overlay2Frame = self.frame;
overlay2Frame.origin.x = round(overlay2Frame.origin.x);
overlay2Frame.origin.y = round(overlay2Frame.origin.y);
self.frame = overlay2Frame;
The other issue I still can't solve. the view in ButtonTest.xib just won't get bigger to match the other view.
See Question&Answers more detail:
os