0

Current implementation of floating button

The following is the current implementation. The grey button is floating on the scroll view. Is there a way to make the button appear once the yellow view (end of scroll view) is reached. Then keep it floating on the screen at the very bottom.

I'm using the following code:

override func scrollViewDidScroll(scrollView: UIScrollView) {

if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
    //reached bottom - how to show button below yellow
    // and keep it floating as shown above
}
}

Adding additional code of what I've tried so far:

    @IBOutlet weak var btnScroll: UIButton!

var startingFrame : CGRect!
var endingFrame : CGRect!

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) && self.btnScroll.isHidden {
        self.btnScroll.isHidden = false
        self.btnScroll.frame = startingFrame // outside of screen somewhere in bottom
        UIView.animate(withDuration: 1.0) {
            self.btnScroll.frame = self.endingFrame // where it should be placed
        }
    }
}

func configureSizes() {
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    startingFrame = CGRect(x: 0, y: screenHeight+100, width: screenWidth, height: 100)
    endingFrame = CGRect(x: 0, y: screenHeight-100, width: screenWidth, height: 100)

}

override func viewDidLoad() {
    super.viewDidLoad()

    configureSizes()
}

Latest image shown like this. Button is constant and doesn't move

Vinodh
  • 5,262
  • 4
  • 38
  • 68
as diu
  • 1,010
  • 15
  • 31
  • 1
    You can just hide/unhide the button in your if statement that you already calculated and commented? Or is it something else you want to achieve? –  Sep 03 '17 at 19:31
  • @Sneak Thank you for responding! When user is viewing `UIScrollView` contents the button will not appear. It appears as though it was lying under the yellow view. And once it comes on screen it needs to persist as shown in the image above. By unhiding in the above method, won't it abruptly show up on screen rather than appearing as if it was below the yellow view? Thank you again. Could you please help. – as diu Sep 03 '17 at 19:34
  • 1
    Well, you can just simply use an UIView animation method to animate it to the screen, once the animation is finished you can just keep it there if you dont want to hide it again. Here is an example : https://stackoverflow.com/questions/42326892/uiview-appereance-from-bottom-to-top-and-vice-versacore-animation –  Sep 03 '17 at 19:37

1 Answers1

1

If I understand you right you want to put button on the position which shown on the gif

Try this code:

override func scrollViewDidScroll(scrollView: UIScrollView) {
  if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) && self.button.isHidden {
    self.button.isHidden = false
    self.button.frame = startingFrame // outside of screen somewhere in bottom
    UIView.animate(withDuration: 1.0) {
      self.button.frame = yourFrame // where it should be placed
    }
  }
}

UPDATE

add this code to hide your button before animation will show it

override func viewDidLoad() {
    super.viewDidLoad()
    self.button.isHidden = true
    ...
}
Yerken
  • 299
  • 1
  • 12
  • thank you. could you please check the updated code above. The view currently has no storyboard constraints whatsoever. Still the desired result is not coming up. – as diu Sep 03 '17 at 20:49
  • the grey button constantly appears on screen even without any scrolling performed. I have removed all storyboard auto layout constraints to it. I will attach a screenshot of how it is looking currently. – as diu Sep 03 '17 at 21:01
  • Added scrollview IBoutlet and added its delegate to self in viewdidload. Now after adding your code to the above changes, the button animation happens soon after the screen loads. Even without scrolling the button animation happens. Any way to control it only when scrollViewDidScroll()? Thank you! – as diu Sep 03 '17 at 21:31
  • 1
    It's because your code (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) is always true, recalculate it, and be sure that it works correct – Yerken Sep 03 '17 at 21:52