How to change UICollectionView frame on button touch - ios

I am trying following code but nothing works :
CGRect frame = collectionview.frame;
collectionview.frame = CGRectMake (frame.origin.x, frame.origin.y+100, frame.size, frame.size-100);
AutoLayout is set to true for the project.

Try below code:
CGRect frame = collectionview.frame;
collectionview.frame = CGRectMake (frame.origin.x, frame.origin.y+100, frame.size.width, frame.size.height-100);

Try this one :
CGRect frame = collectionview.frame;
frame = CGRectMake (frame.origin.x, frame.origin.y+100, frame.size, frame.size-100);
collectionview.frame=frame;

Related

changing just the height of the UIImageView

how can i set the height of UIImageView and leaving other attributes as it is from the .xib? i have UIImageView in .xib file but i only want to set height of it programmatically. can it be done?
Yes It can be done. For eg: If you have UIImageView as imageView then do the below to change the specific frame value.
Way - 1
CGRect rect = imageView.frame;
rect.size.height = /* YOUR_HEIGHT */;
imageView.frame = rect;
Way - 2
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, /* YOUR_HEIGHT */);
I highly recommend you to add https://github.com/AlexDenisov/FrameAccessor to your project. It allows to work with frame directly, for example:
view.x = 15.;
view.width = 167.;

How can I resize an UIView by code?

For Example:
I can get the height of UIImageView(identifier:ImgView) by the top and bottom constraints and I want to let its width be (4/3 x height).
How can I do it?
I have ever tried doing it by this sentence:
ImgView.frame.width = ImgView.frame.height * 4.0 / 3.0
But it didn't work with an error said:
it can't assign to ImgView.frame.width
So, how can I achieve this?
Use a ratio constraint. Easy peasy.
Try this:
CGRect frame = ImgView.frame;
frame.size.width = frame.size.height * 4/3;
ImgView.frame = frame;
Or a one-liner:
ImgView.frame = CGRectMake(ImgView.frame.origin.x,
ImgView.frame.origin.y,
ImgView.frame.size.height * 4/3,
ImgView.frame.size.height);

Autolayout UITextView inside UIScrollView size not correct

So I know there are already a lot of questions about this subject, but I haven't found one that has solved my problem (or perhaps I don't understand the answer).
Alright, so I have set up a scrollview that has an UIView in it, containing an image view at the top (a gradient view which you can ignore), and a textview under it. The textview has to expand with whatever is put inside of it without scrolling (hence why it's in the scrollView).
I have in code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setupGradientView];
[self resize];
}
- (void) resize {
CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
CGRect newFrame;
// reset width for scrollview
newFrame = imageView.frame;
newFrame.size = CGSizeMake(maxWidth, imageView.frame.size.height);
imageView.frame = newFrame;
gradientView.frame = newFrame;
newFrame = textView.frame;
newFrame.size = CGSizeMake(maxWidth, textView.frame.size.height);
//textView.frame = newFrame;
newFrame = dummyView.frame;
newFrame.size = CGSizeMake(maxWidth, dummyView.frame.size.height);
dummyView.frame = newFrame;
// resize height
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
newFrame = textView.frame;
newFrame.size = CGSizeMake(fixedWidth, newSize.height);
//textView.frame = newFrame;
self.textViewHeightConstraint.constant = newSize.height;
CGFloat dummyViewHeight = textView.frame.origin.y + textView.frame.size.height;
dummyView.frame = CGRectMake(dummyView.frame.origin.x, dummyView.frame.origin.y, dummyView.frame.size.width, dummyViewHeight);
[scrollView setContentSize:dummyView.frame.size];
}
Unfortunately, the content size seems to be off by what I think may be 16pts, resulting in a view that looks like this with the text cut off (don't mind the unicode mess in the text):
Here's a picture of what the view looks like in IB: https://i.imgur.com/hxzzUg2.png
Here's what the constraint hierarchy looks like: https://i.imgur.com/rUepwa2.png
Any help would be greatly appreciated!
EDIT: If I comment out the entire code in the resize function, I get the same result. It would appear that auto-layout is resizing all of this after the function is called...but I thought auto-layout was completed when viewDidLayoutSubviews was called?
Edit 2: I have tried and found that adjusting the frame of the textView even n viewDidAppear has no effect. (I can edit things such as the background color)
What I ended up having to do was setting equal width and equal height to the view (not the scrollview), and then manually changing the height constraint in viewDidLayoutSubviews, along with changing the frame of the textView and the content size of the scrollView.

Changing just 1 value in a view frame

I am somewhat new to iOS and to change the width or height of a view I am using the following code:
view.frame = CGRectMake(view.frame.origin.x,
view.frame.origin.y,
view.frame.size.width,
newHeight);
This feels wrong because I am copying all of the old values into a new rect just to change a single value, but I can't just do:
view.frame.size.height = newHeight;
Is there a better way to do this?
You can't assign the values directly. You have to create a variable for it:
CGRect frame = view.frame;
frame.size.height = newHeight;
view.frame = frame; // or [view setFrame:frame];
Another way which helps you keep the frame variable in a different scope:
view.frame = ({
CGRect frame = view.frame;
frame.size.height = newHeight;
frame;
});

Move uiview once uilabel text has changed

I have a UIlabel that when a user clicks a button its text changes, this label is then resized using:
CGRect frame = label.frame;
[label sizeToFit];
frame.size.height = label.frame.size.height;
label.frame = frame;
So that the width is kept the same but the height of the label is changed so all text fits in this.
I then however need the uiview below this label to be moved down so that it starts at the bottom of the label, how do I do this?
Worked it out in the end by doing:
CGRect contentframe = _lbl_content.frame; //the uilabel
CGRect keyframe = _view_keystuff.frame; //the uiview
float startlocation = contentframe.origin.y;
startlocation += contentframe.size.height;
keyframe.origin.y = startlocation;
_view_keystuff.frame = keyframe;
You just want to put the UIView below the label?
Try this:
UIView *viewBelowLabel = [[UIView alloc] init];
viewBelowLabel.frame = CGRectMake( "your view origin x", label.frame.origin.y + label.frame.size.height, "your view size width", "your view size height");
You should adjust UIView frame after the resizing of the label

Resources