Why is my UIWebView frame not resizing properly? - ios

I did this exact same thing with a XIB and it works, but something is wrong with my storyboard version.
Here's my UIWebView Delegate code
#pragma mark - UIWebViewDelegate Protocol Methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = webView.scrollView.contentSize.height;
_webView.frame = frame;
NSLog(#"Webview Frame height is %f", _webView.frame.size.height);
_scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, _headerImageView.frame.size.height + webView.frame.size.height);
NSLog(#"scrollview content height is %f", _scrollView.contentSize.height);
}
NSLog is showing proper webview frame height of about 800 points. Scrollview content size is perfect about 1000 points.
My Scrollview is set to have a red background. So you can see here the webview frame is only about 500 points on screen and the rest of the content is getting cut off.
Any ideas? Thanks!

The problem is that auto layout is responsible for both the frame of the web view and the content size of the scroll view. You can set them, and of course if you then read them, you read what you just set; but you're just misleading yourself, because then (outside your method) auto layout comes along and sets them for real, and what you are seeing (as opposed to your meaningless NSLog readings) is the result of auto layout's settings. You need to configure your constraints properly so that auto layout does what you want it to do.
Auto layout wasn't turned on in the .xib file, so you didn't encounter this behavior. But it's turned on in your storyboard.

Related

UITableView frame size strange behaviour

I try to set frame size to my table view programmatically after viewDidLoad:
[_tableView setFrame:CGRectMake(0, topHeight,
[[UIScreen mainScreen] bounds].size.width,
[[UIScreen mainScreen] bounds].size.height - topHeight)];
Default size is 200x200 with origin Y 100:
Scene hierarchy:
Implementation of UITableView shows when setFrame was called:
#implementation MainTableView
- (void)setFrame:(CGRect)frame {
NSLog(#"setFrame: %f %f", frame.size.width, frame.size.height);
[super setFrame:frame];
}
#end
All is fine but table's frame resizes to default when I press any cell without printing log messages from setFrame. Small explanation video: http://youtu.be/JUW_rHvCS2I
I don't understand why my table size becomes default (200x200) after cell click. Even if I try to set size inside viewWillAppear (after return from cell detail view) it doesn't work.
Any ideas?
I can't really tell why your tableView isn't resizing properly since you haven't provided much code to look around. Though I recommend setting some constraints or resizing rules for the tableView, probably something like this:
http://i.imgur.com/N9fvvIB.png (if not using Auto Layout)
http://i.imgur.com/3Uz8e5e.png (if using Auto Layout)
Now if you really need to make this work only trough code I suggest throwing up some NSLog's containing the tableView's frame width and height before and after setting it up.
Try _tableView.translatesAutoresizingMaskIntoConstraints = NO.

Change UIImageView height on scroll

I need to resize imageview on scroll and tried some answers in stackoverflow but failed to implement those. Not sure what did I missed, please advice.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
int scrollY = scrollView.contentOffset.y;
if (scrollY > 0) {
_coverImgView.frame = CGRectMake(0, 23 + scrollY, 375, 139 - scrollY);
_coverImgView.center = _coverImgView.superview.center;
}
}
Sharing current configuration of my UIImageView
Also sharing UI
Let me know, your suggestions on this.
Expecting UIImageView to reduce height onscroll.
When using auto-layout you shouldn't be directly setting the frame of any of your views, you should be changing the layout constraint constants to reposition and size your views and then updating the layout.
So, change the 'top' and 'height' constraints each time the delegate method is called, then layoutIfNeeded.

iOS 7 UIScrollView height not change

On an iPhone I have a UIViewController with a UIScrollView and I am having problems setting the height.
[_scrollView setScrollEnabled:YES];
//[_scrollView setContentSize:_viewController.frame.size];
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, 700);
NSLog([NSString stringWithFormat:#"%f", _scrollView.frame.size.height]);
NSLog reports 574.000000
I have done this on iPad development and works fine
If you want to make the frame of the scroll view bigger, use
[_scrollView setFrame:newfram];
If you want to set the scrolling area inside the scrollView to be bigger, use
[_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width, _scrollView.frame.size.height + 300)]; // ) was missing
the previous example adds 300 pixels of extra space to the scroll view. The scroll view will only scroll how you are expecting if the content size is larger than the frame.
The NSLog is correct. The frame.height and contentSize.height properties relate to two different things...
You can think of scrollView.frame.height as the size of the frame in which content appears.
Then consider scrollView.contentSzie.height to be the size of the content displayed within the frame
Hope this helps

problems with scrollview, not scrolling full length

I have a scroll view, added in the IB, and also I added a lot of objects/controls to it on IB itself. Some of them are added to the scrollview but they are out of its bounds.
Back in the class, I'm setting its content size like this
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height + 500);
The scrolling length I observe is not that much. I just observe like 500 px (while the height I set in the content size is around 900 px).
Please tell me what I'm doing wrong.
I use the IB to set the scroll view and the inner controls and I let the scroll view to be longer than the view area it self as why do you need to set it inside the code unless you draw the inner controls dynamically at the runtime ...I hope it is useful

UIScrollView content size clipping subviews when it shouldn't

I have a UIScrollView that contains several dynamically resizing subviews. I can resize and layout the subviews just fine, but when I set the content size of the scroll view itself, the bottom subviews are clipped. Is there some reason why a scroll view's content size height should be larger than the sum of the heights of the views it contains?
Here's my situation in more detail:
I have a superview containing a UIScrollView containing several subviews. In the superview's layoutSubviews method, I calculated the needed size of each subview, then set the frames so the subviews are tiled vertically down the screen with a bit of space between them. When done, I set the height of the UIScrollView's content size to be the end of the last subview (origin.y + size.height). In theory, this means the bottom of the scroll view's content area should exactly line up with the bottom of the last subview.
But it doesn't. Instead, a nice chunk of the last subview is clipped. It's still there - if I scroll down I can see the remaining portion during the "bounce". The problem is even worse in landscape mode - a much larger portion of the bottom subview simply isn't visible.
The subviews are all being arranged and positioned properly. The problem is that the UIScrollView's contentSize seems to need to be significantly larger than the sum of the heights of the subviews (plus the space between them). This doesn't make any sense to me. Furthermore, the amount the size is "off" varies - I reuse this view several times with different subviews, and they're all off by a different amount. Therefore, simply adding a constant to the content view height won't help.
What is causing the content size (or my height calculations) to not function correctly?
Code:
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.bounds.size.width - [self subviewLeftMargin] - [self subviewRightMargin]; // All subviews have same width as parent view
CGFloat x = [self subviewLeftMargin]; // All subviews should start at the far left of the view
CGFloat y = [self spaceBetweenSubviews]; // Running tally of the y coordinate for the next view
/* Adjust the subviews */
for(UIView *view in self.theVariousSubviews) {
/* Resize the view with the desired width, then let it size its height as needed */
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, width, view.frame.size.height);
CGSize newSize = [view sizeThatFits:view.frame.size];
/* Set the origin */
//The subviews are positioned correctly, so this doesn't seem to be a problem
view.frame = CGRectMake(x, y, newSize.width, newSize.height);
/* Have the view refresh its own layout */
[view setNeedsLayout];
/* Update the y value for the next subview */
y += newSize.height + [self spaceBetweenSubviews];
}
/* Resize the scroll view to ensure it fits all of the content */
CGFloat scrollViewHeight = y;
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, scrollViewHeight);
//Content size is set to the same total height of all subviews and spacing, yet it is too small. Why?
}
hi it seems to me that your calculation and resizing timing is wrong.
Without the missing code for the layout change I could not fully understand the problem.
What strikes me is that you are assigning view.frame twice and between the new calculation you intercept the process with sublayouting which might change some of the values your calculation is depending on.
I could only advice you to separate the calculation from layouting and not invoke methods while you are calculating. To bring light into it you should either drop a sample app with the missing calculation or for yourself add some NSLog statement showing you the frame origin size of any subview and the contentOffset for the scrollview.
On my experiences the scrollview is working properly in general so I would expect a bug within your code.

Resources