I have a ViewContainer in my ViewControler, and I wanted to ask you guys if it's possible to change the size of the ViewContainer (change the height over a gesture) when the app is running.
Thanks!
You can change size of UIView of viewcontroller using below code
Call this method whenever you need to change frame size of view
Sample code below
Set view height to 100
[self.view setFrame:CGRectMake(0,0,320,100)];
To change height to 80
[self.view setFrame:CGRectMake(0,0,320,80)];
This code is good, but static.
I need it to change the height of the ViewContainer with a gesture with the finger.
Related
In my UIViewController, I have a non-editable attributed UITextView with scrolling enabled, and I would like to resize the height of the scrolling text to accommodate all screen size widths without having extra white space. I am using AutoLayout and set up outlets for the textview and its height constraint. In the view controller's viewDidLoad method I use the sizeThatFits method to update the textview height, but the resulting size is too small.
- (void)viewDidLoad{
[super viewDidLoad];
NSLog(#"INIT HEIGHT: %f, myTextView.frame.size.height);
CGSize sizeToFit =
[myTextView sizeThatFits: CGSizeMake(myTextView.frame.size.width, MAXFLOAT)];
myTextViewHeightConstraint.constant = sizeToFit.height;
[myTextView updateConstraints];
[myTextView layoutIfNeeded];
NSLog(#"NEW HEIGHT: %f", myTextView.frame.size.height);
}
The log indicates the height was indeed changed from my default of 4000 to 2502, but with 2502 I only see about half of my text via scrolling. I am avoiding nesting the text view in a scroll view, as one solution suggests, since this is discouraged in documentation. What am I missing? Thanks in advance.
I found that the frame of a UITextView will automatically size to its content, so dynamically setting it is not necessary. sizeThatFits is a UIView method and doesn't modify the size of the text content, but rather the size of the window that the text appears in. The only code I needed was to scroll the text to the top:
[myTextView scrollRectToVisible: CGRectMake(0,0,1,1) animated:NO];
This, as well as any Autolayout constraint updates shouldn't be made in viewDidLoad, but rather in viewDidLayoutSubviews or viewWillAppear. Best placement could probably be explained better - I'm open to comments.
I have a UIImageView as a subview of UIView (frame size 300 x 300). My UIImage would be either portrait, landscape or some odd sizes. How do I fill in the UIImage within the UIView frame size without stretching the image to 300 x 300?
The purpose of this, I will be adding another UIImageView as the image border.
Please help.
Thanks.
Try
[img setContentMode:UIViewContentModeScaleAspectFit];
UIViewContentModeScaleAspectFit: Scales the content to fit the size of the view by
maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
or [img setContentMode:UIViewContentModeScaleAspectFill];
UIViewContentModeScaleAspectFill: Scales the content to fill the size of the view. Some
portion of the content may be clipped to fill the view’s bounds.
if you are clipping subviews, then you need to do
[imgView clipToBounds:YES];
Ah I just reread what you were asking, I know what your question is now.
Your UIImageView's frame changes, and when it does so does your image. You don't want your image to change, but you do want your ImageView to adjust to fill the view it is contained in.
UPDATE
I figured it out.
[self.imageView setContentMode:UIViewContentModeCenter];
No matter what orientation, the size stays the same.
You also have the option of setting it to align top, bottom, left, right, top left, top right, bottom left, bottom right, all of which only help to align your image and NOT redraw or re-size the image.
Does that help?
[image setContentMode:UIViewContentModeScaleToFill];
Set the contentMode property of UIImageView to either of this values depending on where you want to put it in the superview:
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
As this values:
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
Will indeed cause the image to be 300 x 300, which is what you don't want.
Use the contentMode property of UIImageView.
Edit: and set the image size to 300x300.
I have a vertical CCScrollView on iOS where height is not divisible by cell height.
So, cells align to the bottom of CCScrollView.
Is there any way to make CCScrollView align to top?
I have not used the CCScrollView, but I have tried another UIScrollView implementation for cocos2d and I think the same way I solved this could apply. Set your scrollview's contentOffset to ccp(0.0, scrollview.contentSize.height - scrollview.size.height) instead of ccp(0.0, 0.0). Then, adjust the position of your content based on your scrollview's contentSize. For instance if you wanted your content's position to be ccp(x, y) as it appears, you could do something like
content.position = CGPointMake(x, scrollview.contentSize.height - scrollview.size.height + y)
size is the view size of the scrollview, but I'm not sure what the actual property for scrollview is since I haven't used it.
Use below code works for me:
[self.scrollView setContentOffset:ccp(0, -self.scrollView.container.contentSize.height + self.scrollView.boundingBox.size.height) animated:NO];
I've a Storyboard with a UIScrollView which contains two UILabels, a UIImageView and a UITextView. The content of the UIImageView and UITextView is dynamic and so are their height.
Currently I'm doing this inside my viewDidLoad to adjust the size of the UITextView after the dynamic text is inserted:
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;
Is this the way to change its height?
My next problem is to set the content size for the UIScrollView, to activate the scrolling. Is there a smart way to get the height of all its content or do I have to calculate the height for each element and set the sum of this as the content size of the UIScrollView?
IF you had no space in between your objects, you could make a for loop in your scrollView.subviews and add up all the heights to set as the contentSize.
As you probably don't have everything tight together, you're probably better by getting the bottom most object and adding up it's frame.origin.y and it's frame.size.height (maybe you want to have some extra space in here, but that's up to you) and that will give you your contentSize.height to keep everything in there.
How would I go about animating open a textviewto expand when clicked? So if I have a UITextView that is of height 30 but when it gains focus have it expand to 100. Then on losing focus have it go back to 30 even if the text is of height 100.
TIA
Look into the UITextViewDelegate methods -textViewDidBeginEditing: and -textViewDidEndEditing:
Interesting you ask this, because I literally just figured out how to do the same thing. Call this method whenever you want to resize your textview:
-(IBAction)resizeFrame {
textView.frame = CGRectMake(x, y, width, height);
[self.view bringSubviewToFront:questions];
}
Line 1 sets the frame of the textView as a rectangle starting at point x,y with whatever width and height you specify.
Line 2 brings this view to the front so that you can actually see it!
If you want to size it back down, call another method with a different name that sets the CGRect back to the textView's original size.