Resize imageview in its frame - ios

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];

Related

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

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

Scrollview is not occupying the entire screen width for s custom tab

Im using UITabBarController with custom tabs.
Im having the following code in viewDidLoad method.
[[NSBundle mainBundle] loadNibNamed:#"CustomTabUIView" owner:self options:nil];
_enclosingView.frame=CGRectMake(0, self.view.frame.size.height-49, self.approveStatutoryButton.frame.size.width * 2, 30);
_scrollView.contentSize=CGSizeMake(_approveStatutoryButton.frame.size.width*2, 0);
[self.view addSubview:_enclosingView];
There are two buttons of width 244 and height 49. So im making the scrollview and the enclosing view to fit that size.
But the problem is that,
scrollview is not taking up the entire screen space so the buttons are hidden a little.
I want this to be adaptive to all devices. will it be the correct approach or is there any third party libraries that I can make use?
[[NSBundle mainBundle] loadNibNamed:#"CustomTabUIView" owner:self options:nil];
CGSize * windowSize=[[[UIApplication sharedApplication] delegate] window].frame.size,
_enclosingView.frame=CGRectMake(0, windowSize.height-49, windowSize, 30);
_scrollView.contentSize=CGSizeMake(windowSize*2, 30);
[self.view addSubview:_enclosingView];
Note:Window size contains the width of the whole screen use it appropiately

Want xib file to cover whole screen instead of covering top left ios

I am making an ios app and using xib files instead of storyboards. At first I didn't even have to use auto layout everything worked fine for all iphone devices in portrait. But as soon as I added a launch screen through assests only the the iphone 5 has the correct layout (because xib file size is set to 4 inch). But any greater screen size and only the top left hand corner is covered like below:
I want the view to cover the whole screen. There is a view at the top but I cannot add any constraints to it. Inside the view there is an imageview that is supposed to cover the whole screen. So I added 4 constraints and pinned the 4 sides to 0 so the imageview should cover the whole screen. But it still does not work. What can I do so the whole screen will be covered?
Have you tried set the frame?
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"xibFileName" owner:nil options:nil];
[[array objectAtIndex:0] setFrame:self.view.frame]; // Trick here: set the main view frame
This worked in my case:
// Create a Container
UIView *container = [[UIView alloc] initWithFrame:self.view.frame];
container.backgroundColor = [UIColor clearColor];
// Add .xib file on top of the screen
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"xibFileName" owner:nil options:nil];
[container addSubview:[array objectAtIndex:0]];
// Add container to main view
[self.view addSubview:container];
About the itens inside of .xib file you have to set the constraints there.

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.

Resources