How to add an UIView to a table cell in objective-c? - ios

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.

Related

How to keep the autolayout of xib then add this to a scrollView?

I have an autolayout xib with constraints added in my application.
Then I add my xib view to a scrollView lateral like a pageController.
But when I add this to the scrollView I get a lot of error with constraints and the outlets isn't showed properly. "[LayoutConstraints] Unable to simultaneously satisfy constraints."
This is my xib class code:
#implementation InvitView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
nibname=#"Invit";
[self addSubview:
[[[NSBundle mainBundle] loadNibNamed:nibname owner:self options:nil] objectAtIndex:0]];
}
return self;
}
#end
And I add my xib to the scrollView with this code:
- (void)viewDidLoad {
[super viewDidLoad];
float width = self.scroll.frame.size.width;
float height = self.scroll.frame.size.height;
int count = 0;
InvitView *myView = [[InvitView 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)];
[myView setNeedsUpdateConstraints];
[self.scroll addSubview:myView];
}
I'm trying set translatesAutoresizingMaskIntoConstraints = NO; but with this code I lost all constraints, and I want keep this then add to the scrollView.
How can I keep my initial constraints in the xib?
EDIT
On the one hand, I have this constraints to my UIScrollView in my viewwController:
On the another hand I have this in my xib:
And the exactly log is:

initWithCoder: Custom View - Determine view controller that is being instantiated

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!!!

Frame not being set with initWithFrame with xib

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

Show view from xib in ScrollView

firstly I want to say that I am newbie at iOS development. I have a problem with showing a view from xib file in ScrollView.
This MyView is only green background with one label. Here is a code from its controller:
#implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//init code
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
}
return self;
}
#end
And here is a code from main controller where is the ScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 67, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
scroll.frame = CGRectMake(0, 20, 320, 350);
scroll.contentSize = CGSizeMake(320, 350);
scroll.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scroll.bounces = YES;
scroll.bouncesZoom = YES;
scroll.scrollEnabled = YES;
scroll.minimumZoomScale = 0.5;
scroll.maximumZoomScale = 5.0;
scroll.delegate = self;
scroll.pagingEnabled = YES;
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil];
UIView *myView = [nibContents objectAtIndex:0];
myView.frame = CGRectMake(0,20,300,300);
[scroll addSubview:myView];
}
Only thing I want to do is that I want to see MyView (green background with one label) inside the ScrollView. If I run this app, scrollview is empty, nothing is shown inside.
Probably it is a stupid question but as I said, I am newbie at this. I have a PageControll app from Apple but for me this is quite complicated code in my iOS beginning. Of course I was googling... Please tell me where is a problem. Thank you
Where do you define scroll? Is it a property of your viewcontroller?
I think the problem here is that you don't add scroll view to your viewcontroller's view with
[self.view addSubview:scroll]
Everything seems to right. Just retain the view in your class that you are receiving from the following line and add that as a subview to your scrollview.
self.myView = [nibContents objectAtIndex:0];
Best Regards.

How to position a View as Subview?

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.

Resources