iOS Swift: UIScrollView show scrollbar if needed (content height exceeds visible height) - uiscrollview

I wanted to show the UIScrollView's scrollbar only when needed, i.e. when the content exceeds the visible size and needs scrolling to see everything.
So I added this code:
self.view.addSubview(self.myScrollView)
self.myScrollView.edgesToSuperView()
print(self.myScrollView.contentSize.height)
print(self.myScrollView.bounds.height)
if self.myScrollView.contentSize.height > self.myScrollView.bounds.height {
self.myScrollView.showsVerticalScrollIndicator = true
}
However, contentSize.height and contentSize.height always print out as zero, although that inside the scrollView there is a stackView that has many text labels that exceeded screen height. So this didn't worked out.
Any better ideas?

This property:
.showsVerticalScrollIndicator = true
only controls the visibility of a scroll indicator while the user is scrolling.
By default, if your content is not "tall enough" to need to scroll, the scroll indicators are never shown.
If your content IS "tall enough" to need to scroll (height is greater than the scroll view's frame height), the scroll indicators are shown while scrolling.
There is no way to have them "always showing."
If you want to indicate to the user that there is more content to be seen by scrolling, you could add something like "arrows" that you show / hide as appropriate, or, you can call `self.myScrollView.flashScrollIndicators() to "flash" the scroll indicators.

Related

How to fix NSTextView's weird sizing inside a magnified NSScrollView

I have an NSTextView inside NSScrollView/NSClipView, the usual setup. When I magnify the scroll view with [NSScrollView setMagnification:...] and resize the window, the width of text view's frame gets constantly larger, regardless of whether I stretch or shrink the window.
If the scroll view is not magnified, text view behaves normally. I have tried removing constraints and disabled subview autoresizing, but nothing helps. Whenever i set any sort of magnification, text view size changes on every call to resize. If the magnification is under 1, it shrinks.
Any bugs in TextContainer shouldn't make it wider either, as I've set textContainer.widthTracksTextView = false;
I am trying to keep the textContainer centered in my NSTextView by setting insets to it, but it gets impossible with the varying sizes. I've gone through my code and nothing should make it resize. Is this a bug or does the setMagnify: cause problems with constraints or some other math in LayoutManager?
For anyone trying to figure this out:
This is caused by a minimum width constraint for the magnified view, or any of its subviews. Seems like a bug, as I can't wrap my head around how this would be intended behaviour.
Just remove the >= constraint and do something to limit the size in run-time if needed.

iOS11 paged scroll view suddenly is scrollable vertically

I have an app which uses EMPageViewController to display a set of onboarding slides. My understanding is that the underlying scroll view is using paged mode to display slides.
Upon update to iOS11, I see that suddenly the slides follow the finger, so they are draggable and bounce up and down. I expect paged scroll view to be scrollable horizontally only.
How can I restrict paged scroll view to horizontal scrolling only in iOS11 ?
I tried this but it did not work
pageViewController.scrollView.alwaysBounceVertical = false
This fixes the issue:
if #available(iOS 11.0, *)
{
self.scrollView.contentInsetAdjustmentBehavior = .never
}
The behavior for determining the adjusted content offsets. This
property specifies how the safe area insets are used to modify the
content area of the scroll view.
contentInsetAdjustmentBehavior is new from iOS 11 > and the default value is automatic.
Content is always adjusted vertically when the scroll view is the
content view of a view controller that is currently displayed by a
navigation or tab bar controller. If the scroll view is horizontally
scrollable, the horizontal content offset is also adjusted when there
are nonzero safe area insets.
Which made some of my UIScrollView scroll more than they were excepted to.

How do you tell if a UIScrollView content overflows

I have a UIScroll view with content in it. If that content overflows, making the scroll view scrollable, I'd like to set the bottom of the view to be a certain color. If it does not, I'd like to set it to a different color.
My issue is, I do not know how to detect if a UIScrollView's content overflows and thus is scrollable.
Check if the scroll view's contentSize is bigger than its bounds.
I know it's an old question, but my scroll view had a content insets set which made the accepted answer slightly off. Here's how to take the content inset into account:
if scrollView.contentSize.height + scrollView.contentInset.bottom > scrollView.bounds.size.height {
// Scroll view is able to scroll.
}

UIScrollview scroll not working

UIScrollview scroll is not working when i tested application in device (iPhone 5), But scroll is working properly in iPhone 5/5s/6/6 Plus simulator. I am developing application using Xcode 6 and UIStoryboard with AutoLayout.
I set the top, bottom, left and right constraints of UIScrollview with relative to Superview. And i also set the constraints of every UIControl which is in the UIScrollview.
Has any one have solution regarding this issue?
Please see below screenshot for reference.
It's probably easiest to have the scroll view contain a single view, which then contains all of your scrollable content. That single view should be anchored to all 4 sides of the scroll view, and if it's only meant to scroll vertically (which is what your screenshot makes it look like), then set the content view to be the same width as the parent of the scroll view.
To make sure an UIScrollView scroll, you have to check
Size
Your content View is bigger than your Scroll View. If you want it to be scrolled vertically, make sure its higher; horizontally, wider.
Constraints
Make sure your Scroll View is pinned to its super view, Top, Bottom, Leading, Trailing.
Make sure your Content view of Scroll View DOES NOT have CenterY constraint set to Scroll View, cause that'd make content view stuck with Scroll View and it'd never be able to scroll. (That happens to me the last time I was stuck)
Interface Builder settings
Make sure the check in Interface Builder under Scroll View is checked:
Scrolling section, Scrolling Enabled.

NSScrollView do not display the horizontal scroll bar if displayed from a plugin in a browser

I am developing a web application that uses a plugin to display some native Content. I create a NSView(say A) and return the CALayer of the NSView to the browser for hosting on the browser window. Inside this NSView I host a NSScrollView; The ScrollView contains the actual document that needs to be scrolled(the size of the document is big enough that both the horizontal and vertical scroll bars need to be displayed). I am setting the bounds of the Host NSView(A) as the frame size on the NSScrollView properly; Also I made sure on the ScrollView I am setting setHasHorizontalScroller and setHasVerticalScroller as YES.
Though the veritical scroll bar is displayed in the viewer, the horizontal scroll bar is not getting displayed. I want to know what the error I am making and why the horizontal scrollbar is not displayed. Any clues/suggestions on this is greatly appreciated.

Resources