I am trying to replicate something similar to Android's collapsing toolbar (see this link to understand the effect I am trying to achieve). I found a nice turnaround here.
My main UIScrollView is named fgScrollView and the secondary which is pinned on top is named bgScrollView. The whole UIViewController is embedded in a UINavigationController.
I pin the fgScrollView to the Container View. I pin the bgScrollView top, right and left to the fgScrollView and I give it a steady height of 220, and I place inside the bgScrollView a UIImageView which I also pin to all sides and I give it a height of 220. (All pins are with 0 distance)
This is how it looks from the IB (the UILabel that you see in the hierarchy is placed there to give a scrollable height to the fgScrollView):
In the scrollViewDidScroll method I set:
bgScrollView.contentOffset.y = scrollView.contentOffset.y * 1.4
(I have already set only the fgScrollView to be the delegate and I chose the 1.4 randomly) and in Interface Builder I choose the Adjust Scroll View Insets option.
The result is this in the beginning:
but as soon as I click to scroll up this happens (you can see the gap - it stops being pinned to the top):
From thereon it stays like that when I scroll to the top.
I uncheck the Adjust Scroll View Insets option and a strange thing that I notice is that the image does not go under the navigation bar, although I have checked the options Under Top and Bottom Bars:
And when I start scrolling this happens:
and it stays like that even when I scroll back to the top.
I tried to fix the issue programmatically. I add the navigation bar and status bar heights and I declare this:
fgScrollView.contentInset = UIEdgeInsets(top: CGRectGetHeight((self.navigationController?.navigationBar.frame)!) + CGRectGetHeight(UIApplication.sharedApplication().statusBarFrame), left: 0,
bottom: CGRectGetHeight((self.navigationController?.toolbar.frame)!), right: 0)
and the result is this:
Any clues?...
Related
I'm experiencing some trouble managing a scroll view embed inside a navigation view controller. I'm using Auto Layout and I'll try to explain the problem the best I can.
I embed a scroll view in the controller's main view and pinned the top, left, bottom and right borders to main view's borders.
I embed a standard UIView inside the scroll view and gave it a fixed height of 800 points. This should act as a container for all my controls (let's name it content view). Then again I pinned its top, left, bottom and right borders to the scroll view's borders. Because the bottom space constraint of scroll view's descendant view was negative, I adjusted it bringing it back to 0.
For the width, I CTRL-dragged from the content view to the main view and added a Equal widths constraint.
Finally, I added an image view and placed it at the top center of my content view adding some further trivial constraint.
The storyboard for the situation I've just depicted is shown above (in the document outline you should see all the constraints I've defined).
My scroll view works, it scrolls fine and the image view is well-centered where it's supposed to be. However, there's a gap between the end of the navigation bar and the start of the scroll view and I can't figure out why. It seems to be as high as the navigation bar, but I have no idea about how to fix it. The image shown below should make you understand what I'm talking about (the content view is highlighted in grey for clarity).
I didn't write a single line of code to achieve this result. I would really appreciate any kind of help and I'm ready to give you all the information you need to help me address the problem.
I think you should uncheck Adjust Scrollview Insets property of your viewcontroller to get rid of this....
you have to uncheck Adjust Scrollview Insets
I have an UITableViewController with an iAD banner on top, under the top bar.
When banned is loaded and device is in portrait it looks good but, when there's no banners or device turn in landscape, between top bar and the first tableView's row there's an ugly empty space.
The best solution would be to move the TableView up but I can't find a way to do that; the best solution I thought was to try resizing the header of section 0 of table's but I can't go under 0; and 0 is too much!
Is there another way to move table, for example, 10 pixels up?
The table view's content inset property can be used to add a padding around its content view. If you set the top for 10 for example, there will be some padding between the table view's top, and its content's top.
Using Swift, you can set the tableView.contentInset.top property directly. In Objective-C, you can set it like so:
self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0)
I am loading an HTML document into a UIWebView with loadRequest:requestURL. The HTML content does not fit entirely into the view's frame. What surprises me that at the time the page has been loaded (when webViewDidFinishLoad:is called) the view's scrollView.contentOffset is different from (0,0). It looks as if the content may have been scrolled down approximately so that its vertical center is aligned with the view's vertical center (see screenshot below).
How can this happen and how can I achieve the desired behavior of having the content's top left corner aligned with the view's top left corner initially? Setting scrollView.contentOffset to (0,0) in webViewDidFinishLoad:` is an option but apparently leads to some flickering.
This is occurring in the context of a UIViewController that contains two sibling UIWebViews below its root view. The two UIWebViews are vertically stacked with a UILabel (appears in screenshot with gray background) in between. (The lower UIWebView is not visible in the screenshot, because it's currently all white.) The view uses constraint-based layout and the current problem concerns the upper UIWebView.
UPDATE Further study has revealed that the visual defect is not due to scrolling in the upper UIWebView but due to its frame.origin.y having been set from 0 to 64 (which is also the combined height of the status bar and the navigation bar) by the time webViewDidFinishLoad is called. I am calling loadRequest:requestWithURL: from the view controller's viewWillAppear: and there is a layout constraint on the upper UIWebView that imposes a vertical space of 0 between its top and the bottom of the Top Layout Guide.
I think what happens is the following: iOS 7 wants UIScrollViews to extend underneath the status and navigation bars when scrolled downwards. Apparently it performs the initial vertical scrolling as a means for preventing the top part of web content from being hidden behind these bars before any scrolling occurs.
I had a layout constraint for a vertical distance of 0 to the top layout guide placed on the UIWebView which led to the wrong vertical distance of twice the height of the status bar plus the height navigation bar from the top of the screen. Replacing this layout constraint by one for a vertical distance of 0 from the superview corrected the wrong.
Scroll indicators in UICollectionView starts after from some points from top and ends before that many points from bottom. I mean it has top and bottom margin from UICollectionView's top and bottom.
Should not it start from top and end at bottom of UICollectionView?
I checked that contentInset.top on UICollectionView and sectionInset.top on UICollectionViewFlowLayout are 20.0 and 0.0, respectively.
See below image. Content start from more than 20.0 pts(which is top inset value) from top.
I solved by setting value of below properties to UIEdgeInsetsZero,
// Set contentInset and scrollIndicatorInset to UIEdgeInsetsZero.
self.tournamentCollection.contentInset = UIEdgeInsetsZero;
self.tournamentCollection.scrollIndicatorInsets = UIEdgeInsetsZero;
Value of contentInset.top and scrollIndicatorInsets.top was 64.0. Similarly for bottom. It was causing the contents to start 64.0 pts from top.
Note that you should check above properties' values only after layout pass is executed, otherwise you might get 0.0 as a value. This was the case with me. You should check in viewDidLayoutSubview: methods.
problem in translucent status bar and in your case also nav bar
if your collectionView starts right under nav bar when rendering to it added value to the top of the physical screen (64)
In its Projects I encountered a curious situation.
A nav bar off translucency, but as soon as I hide it in one of the screens, then added to collectionView magic 20 pixels. Yes, yes, this is a translucent status bar
had to take advice Geek
Building on #kolbasek's answer, this other question's answer shows how to correct it in Storyboard editor ("...select the ViewController, and then untick 'Adjust Scroll View Insets'")
iOS7 adds padding (64px) for the status bar. Therefore when using a Scrollview there is a big camp between the scrollview and the navigation bar. I tried:
self.automaticallyAdjustsScrollViewInsets = NO;
Which does remove the unwanted padding - however the scrollview no longer scrolls.
Is there another way?
* update **
I discovered a quick fix. I was lining up the scrollview and y origin= 64px in the storyboard instead of 0. I put my scroll view to 0px (the navigation bar then covered 64px of my scrollview) and when I ran it on the simulator it was aligned as originally intend ... a bit of a hack tho. I'm looking for a solid solution however.
On your XIB/Storyboard for your view controller, make sure to uncheck the Adjust Scroll View Insets. You may also need to uncheck Under Top Bars and Under Bottom Bars: