load views from xib, and how to change views' frame? - ios

I load a view from a nib, this view has several subviews,and are on the xib.
Once I load this view from nib, I tried to change the subviews frame by using: view.subLabel.frame = CGRectMake(). However, this does not work and I do not know why. I disabled the autolayout property of the xib and it does not work as well. Anyone knows how to change views frame loaded from xibs? Thank you.
codes for init and change frame are as follows:
Init:
GADNativeContentAdView *adView = [[[NSBundle mainBundle] loadNibNamed:#"loadingContent" owner:nil options:nil] firstObject];
Then change frame:
adView.logoView.frame = CGRectMake(0, 0,50, 50);
but this does not work.

You should work with NSAutolayout and use NSAutoLayoutConstraints if you want to change the size or position of your subviews. For example declare a height constraint as a property, connect it in your xib, and change it this way:
myHeightConstraint.constant = newValue
UIView.animateWithDuration(duration, animations: {
self.view.layoutIfNeeded()
})

user2053760,
When you instantiate a view using
GADNativeContentAdView *adView = [[[NSBundle mainBundle] loadNibNamed:#"loadingContent" owner:nil options:nil] firstObject];
you havent specified its frame so by default it is instantiated with frame (0,0,0,0). Then you went on to change the view which is inside this adView and set its frame to (0,0,50,50).
How do you expect the subView to be visible if the view which holds it itself is of size (0,0) ???
so first change the parent frame as
adView.frame = CGRectMake(0, 0,50, 50);
then you can set the frame for childView,
adView.logoView.frame = CGRectMake(0, 0,50, 50);

try add the code:
[view setAutoresizesSubviews:NO];
Swift:
view.autoresizesSubviews = false

Related

iOS:I use xib with `autolayout` to create a`uiview`, set it to a tableview's header,there comes a strange issue

This bellow is my code:
/* searchBar */
lml_search = [[NSBundle mainBundle] loadNibNamed:#"LMLSearchHeadView" owner:self options:nil].firstObject;
self.tableView.tableHeaderView = lml_search;
And in my xib, I set a backview which is on the xib's view, I set 4 constraints for backview space to xib's view.
At the viewcontroller1 first show, it was normal, but after the viewcontroller1 push to viewcontroller2, and pop to viewcontroller1, the lml_search's height become 0!
This code can prove me:
(lldb) po lml_search
<LMLSearchHeadView: 0x7f9c1e33f4e0; frame = (0 0; 320 0); autoresize = W+H; layer = <CALayer: 0x7f9c1e33d980>>
I don't know why it comes for my project.
I got the solution, but I must set the xib's view frame in the viewWillAppear::
[lml_search setFrame:CGRectMake(0, 0, kScreen_Width, 44)];
But I don't know the real reason of this issue.

Xcode Custom view constraints not updating when updating size programmatically

I have a custom UIView that has 2 UILabel's which I have designed in the XIB. Have set the correct constraints to the sub items making them right aligned etc. In the XIB, my desgin was based on the assumption that screen width is 200 and constraints of the 2 labels are set to their superview accordingly.
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[NSBundle mainBundle] loadNibNamed:#"MyCustomView" owner:self options:nil];
[self addSubview:self.view];
//_intrinsicContentSize = self.bounds.size; // tried to return this as well
}
return self;
}
Now I programmatically alloc this view and and add it as subview to my view controller. If I change the frame of the view to say 300, the sub view frame gets changed to 300 but the 2 labels which have constraints follow a width of 200 only.
vewOneTouch = [[OneTouchView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(vewBottom.frame)-50, CGRectGetWidth(scwBaseView.frame), 50)];
[scwBaseView addSubview:vewOneTouch];
When the custom view frame is changed, why are the constraints not kicking in and changing the layout of all the items in the subview?
The white area is the yellow view in the Xib, have changed the color programmatically.

Unable to change height of UIView loaded from nib

I have a view that is loaded from a nib like so:
SLSCaseCard* caseCard = [[[NSBundle mainBundle] loadNibNamed:#"SLSCaseCard" owner:self options:nil] objectAtIndex:0];
I'm setting it's frame like this:
cardFrame = CGRectMake(25,25, caseCard.frame.size.width, 44);
[caseCard setFrame:cardFrame];
[self.view addSubview:caseCard];
The x and y are changing but the height always remains the same. The view has a size of 224 in the nib, and it stays at that height at runtime.
Storyboard (or XIB's) generates code for you. And this code is executed in laying out subviews. So if you want to change frame of a view defined in XIB, you should do that after viewDidLayoutSubviews or layoutSubviews.
Hence even after you setting the height of the view, your changes are overridden by the code generated by XIB, which overrides the height to the height defined in XIB, in your case 224. Same goes with width.

Resize imageview in its frame

I am trying to create a view that has 4 children
1 imageview
3 labels
The ImageView is supposed to take up the entire frame, and it doesnt
I've added layout constraints to it so that it would adjust to the sides.
However, i am initiating the view with different frames, and the image view size doesnt change
How do i make the image take up the entire purple area, of which it is a subview?
This is my xib file:
and this is how it looks on the device screen
the purple view is the frame i allocated for the given view:
and the green view is the entire screen
my init code:
NSArray *topViews = [[NSBundle mainBundle]loadNibNamed:#"BagView" owner:self options:nil];
[self addSubview:topViews[0]];
[self.shoppingBag setFrame:self.bounds];
NSArray *topViews = [[NSBundle mainBundle]loadNibNamed:#"BagView" owner:self options:nil];
UIView *v = topViews[0];
v.frame = self.bounds;
[self addSubview:v];
fixed the problem
Set the contentMode property of UIIMageView to UIViewContentModeScaleToFill
[your_imageview setContentMode: UIViewContentModeScaleToFill];

Add UIView from xib to UIScrollView

I am making an application, and I started with a single view controller with one view inside. I made my app but I want to have multiple pages and allow the user to swipe between them. I want to use a UISCrollView. How can I set up a UIScrollView so that I can insert my already made view from the xib. I don't want to make a view programmatically because a lot of it is graphical and I don't really know what I'm doing.
Recap: UIScrollView to swipe between views I have made in xib files
in your main view controller, add a scrollview. You can add scrollview to mainview in Xib.
In mainview controller .h file add a property , then load programmatically your views and add to scroll. Set each view position. Set scrollview reference in your main xib properly.
#property(nonatomic, weak) IBoutlet UIScrollView *scrollView;
UIView *view1 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib1" owner:self options:nil];
UIView *view2 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib2" owner:self options:nil];
UIView *view3 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib3" owner:self options:nil];
CGRect frame;
frame = view1.frame;
frame.origin.x = 0;
view1.frame = frame;
frame = view2.frame;
frame.origin.x = view1.frame.size.width + view1.frame.origin.x ;
view2.frame = frame;
frame = view3.frame;
frame.origin.x = view2.frame.size.width + view2.frame.origin.x;
view3.frame = frame;
[scrolView addSubView:view1];
[scrolView addSubView:view2];
[scrolView addSubView:view3];
Declare IBOutlet in your view controller to point to that view.
#property(nonatomic, weak) IBoutlet UIView *myView;
Connect your view from xib to myView.
Load your xib in the view controller viewDidLoad method
[[NSBundle mainBundle] loadNibNamed:#"myViewXib" owner:self options:nil];
Then add myView to your scrollview.

Resources