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;
});
Related
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.
I am trying to show UIPickerView on click on UITextField. I am using a simple method to show the picker as follows:
-(void) showPickerView {
[self.view resignFirstResponder];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
[UIView commitAnimations];
}
It works great if I have not scrolled down. But when I scroll down, my picker is placed on fix position as mentioned in showPickerView method. Now how I can manage it that my picker appears at bottom every time.
Solution 1:
Add your pickerview to your window.
Solution 2:
Implement the scrollview delegate and adjust the frame continuously.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480){
CGRect frame = self.picker.frame;
frame.origin.y = 270;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 226.0;
self.toolBar.frame = frame;
} else if(iOSDeviceScreenSize.height == 568){
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
}
I can just suppose that you are showing your picker inside of a scroll view, right?
And if so, then your picker frame is relative to UIScrollView, not to a screen view.
So you have to take into account your scroll view offset. Something like that:
CGRect frame = self.picker.frame;
frame.origin.y = self.scrollvew.contentOffset.y + 270;
Otherwise you can show your picker outside of the UIScrollView (just add it to a screen view).
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;
I am trying to create a UILabel and I am setting it to the center of the view using label.center = view.center. But I want to move it up by 100 pixels.
How would I go by doing this?
Something like, label.frame = CGRectMake(current-x, 100, 42, 21);
Is this possible? Keep x the same but change the y?
Do this : label.center = CGPointMake(view.center.x, view.center.y - 100);.
CGRect frame = view.center;
frame.origin.x -= 100.0;
label.frame = frame;
I am calling a custom picker in my application when i hit a button.
The picker looks like a bubble so i am pointing it to the button like this :
thePicker.presentFromRect = button.frame;
I need the picker to show 300 pixels down from this button. How can i do that?
How can i add 300 pixels height on the above statement ?
You need to use :
CGRect frame = self.window.frame;
frame.size.height += 30;
thePicker.presentFromRect = frame;
You should create a new frame and edit its height property like so:
CGRect frame = button.frame;
frame.size.height += 30;
thePicker.presentFromRect = frame;
First get the frame of the button so to be able to work on it.
Then change his height as you like, accessing the size attribute.
Finally, reassign the frame to the picker
CGRect buttonFrame = button.frame;
buttonFrame.size.height += 300;
thePicker.presentFromRect = buttonFrame;