XCode8 initWithCoder frame Size - xcode8

Found if use XCode 8 XIB file, the UIView initWithCoder will get frame (0, 0, 1000, 1000) instead of the XIB frame size setting.
Check the diff of XIB xml file it will add
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
and remove rect frame settings such as
<rect key="frame" x="110" y="0.0" width="20" height="17"/>
Removing rect frame setting is the major root cause to get width 1000 and height 1000 in the initWithCoder.
Therefore, any related size calculation, we need to handle in layoutSubviews.

Related

UIViewController with scrollview doesn't scroll

On my storyboard I've placed a UIViewController that has an image on half it's height and the other half has a scrollViewController with other controls in it like labels buttons segments etc.
when running the app, the scroller doesn't scroll, I have user interaction enabled on as well as scrolling enabled and show vertical indicator.
ideas?
//sview.frame = CGRectMake(0, 0, 568, 320);
sview.contentSize = CGSizeMake(4500, 320);
[sview setScrollEnabled:YES];
You need to set the contentSize of your scrollview. Otherwise it won't scroll.

In iOS6, XCode 4.5, UIScrollView is not Scrolling, despite contentSize being set

I've been banging my head against the wall for the last hour trying to get my scrollView to scroll, but to no avail. In viewDidLoad I have
NSURL *url = [FlickrFetcher urlForPhoto:self.photoData format:FlickrPhotoFormatLarge];
NSData *imageRawData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageRawData];
self.scrollView.delegate = self;
self.imageView.image = image;
self.scrollView.contentSize = image.size;
self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
I have the imageView view mode set to top left.
My UIScrollView was created by selecting my imageView, then Editor -> Embed in -> ScrollView.
Anything else I can check/try?
If you have created your scrollview through nib, and if that nib has autolayout feature then it will not let you scroll.
So go utility window of nib.
Select First tab of utility window.
Remove autolayout and run the application
Checklist:
Is image really downloaded? (so it actaullly has size?)
Is scrollview outlet set?
Is imageView added as a subview of scrollView?
Btw. Don't know if this is just sample code or real but if it's real then it's really bad idea to download data synchronously and even worse idea to do it in viewDidLoad.
I guess it because you have image view embedded in scrollview,so its frame is becoming to imageview's frame. when frame size and content size are equal, scrollview wont scroll. Try setting scrollview's frame pragmatically to some fixed rectangle. ScrollView's contentSize's height and width should be greater then scrollview's frame's height and width. Give it some space to scroll:) In your case , they both are equal I guess.

UIImageView fill in UIView

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.

UIScrollView: handling of dynamic content?

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.

Setting the UITextView height to fit the content height in Objective c

I have the code as below for my UITextView set up.
CGRect itemDescFrame = CGRectMake(20, 160, 280, 200);
UITextView *itemDesc = [[UITextView alloc] initWithFrame:itemDescFrame];
...
itemDesc.editable = NO;
itemDesc.scrollEnabled = NO;
...
I have disabled the UITextView scroll bar, however some of the content on the bottom is being hidden. How can I set the UITextView height to fit to the content height?
Thanks
UPDATE:
NSLog(#"frame %#", itemDesc.frame.size.height);
NSLog(#"content %#", itemDesc.contentSize.height);
Result:
2011-10-11 11:00:18.010 abc[1606:207] frame 200.000000
2011-10-11 11:00:18.010 abc[1606:207] content 200.000000
As a subclass of UIScrollView, UITextView has a property called contentSize which, in the case of a UITextView, should be the size that the text occupies after it is is placed in the view. Try setting the height of the text view's frame to the height of contentSize.
Simply add this line of code to force the frame to update its size based on the height of the text it contains:
[self.myTextView sizeToFit];
Note that if you want to add additional padding you can set textContainerInset.
You'll want to call that when the text content changes and upon rotation. To make it easy to see the height adjust, set a background color on the UITextView.