How to hide a UIButton during textView scrolling - ios

I am making an iOS app which contains a detailed text under UITextView.I added a share button in the view beside a UITextView. I want the button to be hidden when the user starts scrolling and return back when scrolling is not detected.
What I did was this...
if(detailDescriptionTextView.isScrollEnabled == true)
{my button.isHidden = true }
The above code hides the button entirely since scrollview is on by default. So what shall I do?

As of UITextView inherits from UIScrollView you can use UIScrollViewDelegate method for this purpose. You need to just set the delegate of UITextView and implement below methods of UIScrollViewDelegate and you all set to go.
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
if yourTextView = scrollView {
yourButton.isHidden = true
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if yourTextView = scrollView {
yourButton.isHidden = false
}
}

UITextView is a UIScrollView subclass. Therefore the UIScrollView delegate method you are using is also available when using UITextView.
you should use scrollViewDidScroll to detect scroll
func scrollViewDidScroll(_ scrollView: UIScrollView) {
button.isHidden = true
}
You can use scrollViewDidEndDecelerating . It will call when textView stop scrolling.
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
button.isHidden = false
}

Related

UIScrollView Delegate Re-Routing - To check the state of UIScrollView

I have a tableView and button on the same view (Siblings),
i need to HIDE the button when the tableView is SCROLLING (Up and Down Direction no problem) and SHOW when it is STOPPED SCROLLING
I tried the following code, but it is not working (Correctly)
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.vcView.hideButton()
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
self.vcView.showButton();
}
After a lot of search i found this article, but it is in Objective C, can anyone help me to convert it in to Swift 3.
Why not simply implement these two delegate methods:
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.vcView.hideButton()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
self.vcView.showButton();
}
If you're only looking to show and hide a button or any view while scrolling, this will work. Just be sure to set the scrollview delegate and it should work without issues.
Theres no need to convert that objc code to swift.
https://screencast.com/t/eTq9Nzfsgs6E

Move SubView of ChildViewController using scrollViewDidScroll

I am using a viewController to handle two ChildViewControllers, each containing a UITableView. Would it be possible to set the the position y of a SubView of viewController (e.g. a UILabel) depending on the scrollView.contentOffset of the current ChildViewController?
It works fine with its own subviews already,..
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.testConstt.constant = scrollView.contentOffset.y
}
Thanks for helping!
Just observe the correct scroll view using a conditional statement. I assume the scroll views of the children are table views, so you may do something like this:
let tableViewA = UITableView(...)
let tableViewB = UITableView(...)
let someScrollView = UIScrollView()
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tableViewA {
// observe a specific table view's scroll view and do something
} else if scrollView == someScrollView {
// observe a specific scroll view and do something
}
}
Remember, UITableView is a subclass of UIScrollView so they can be treated the same in scrollViewDidScroll(_ scrollView:).

How to scroll two UITableViews symmetrically?

In my app I have two table views. Both are same height and width and same cell count.
I want when tableview A Scroll Simultaneously tableview B also scroll.
You can use scrollview delegate scrollViewDidScroll for this purpose. This will do the exact thing you want.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.tableA{
self.tableB.setContentOffset(self.tableA.contentOffset, animated: false)
}
else if scrollView == self.tableB{
self.tableA.setContentOffset(self.tableA.contentOffset, animated: false)
}
print("scrollViewDidScroll") // to check whether this function is called while scrolling
}
TRY THIS:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
<your tableView Name>.contentOffset = scrollview.contentOffset
}
As Tableview inherits from ScrollView. Try this.
FOR SWIFT 4:
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
<your tableView Name>.contentOffset = scrollview.contentOffset
}
Instead of using tableview you can use UIPickerView with two components.
If you need code let me know.

How can I disabled horizontal scrolling with a UIScrollView on page 2 of 3?

I have a horizontal UIScrollView with paging enabled containing 3 view controllers. The middle view contains touch handling that sometimes gets interpreted as scrolling and runs the user experience. I have navigation buttons for moving to page 1 and 3 but want to preserve the scrolling abilities when in page 1 and 3 since they don't have multitouch/complex touch behaviors.
You can achieve this by implement the delegate method func scrollViewDidScroll(_ scrollView: UIScrollView)
here are some example code in Swift:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x == view.frame.width {
scrollView.isScrollEnabled = false
} else {
scrollView.isScrollEnabled = true
}
}

UIScrollView in UITableViewCell doesn't zoom at touch origin

I have a UIScrollView inside a UITableViewCell to allow zooming on a UIWebView inside the cell. When the view is zoomed in, the UITableViewCell is enlarged to fit the new size of the web view after being zoomed in. This is being done by changing the height constraint on the UIScrollView and implementing UITableView's automatic row height.
The issue right now is that zooming inside the scroll view does not zoom at the origin of the pinch, but somewhere above, so the view does not get zoomed in where the user intended.
Here is the view hierarchy:
[UITableView]
[CustomUITableViewCell]
[UIView] (Content View)
[UIView]
[UIScrollView]
[UIWebView]
[UIView]
The cell is both a UIWebViewDelegate and a UIScrollViewDelegate:
Custom UITableViewCell:
func configure() { // Called from tableView:cellForRowAt:
scrollView.delegate = self
webView.delegate = self
webView.scrollView.isScrollEnabled = false
webView.scalesPageToFit = true
webView.scrollView.bounces = false
webView.scrollView.clipsToBounds = false
let htmlTemplate = TableViewCell.messageTemplate
webView.loadHTMLString(htmlTemplate, baseURL: Bundle.main.bundleURL)
}
// UIScrollViewDelegate
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return webView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
updateWebViewFrame()
scrollViewHeightConstraint.constant = webView.frame.height
layoutIfNeeded()
delegate?.updateCellHeights()
}
The table's view controller is the delegate for the cell, which implements updateCellHeights:
func updateCellHeights() {
// https://stackoverflow.com/a/30110230/482536
let offset = tableView.contentOffset
tableView.beginUpdates()
tableView.endUpdates()
tableView.layer.removeAllAnimations()
tableView.setContentOffset(offset, animated: false)
}
How do I make the scroll view zoom to the pinch origin?
Is this even the right approach to this problem? I tried using the scrollView inside the UIWebView directly, but couldn't get the UIWebView to resize to the inner scrollView correctly.

Resources