Skip to content

Calculate scroll view frame correctly #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2018
Merged

Calculate scroll view frame correctly #72

merged 2 commits into from
Sep 22, 2018

Conversation

jasonaibrahim
Copy link
Contributor

Hi @rnystrom

Thank you for putting this repo together, it has been very useful.

I encountered an issue when adding a UITableView as a separate view to an existing UIViewController when the table view was positioned somewhere other than the top of the view.

In the layout method, I noticed the scrollView was getting its frame from the view bounds, but this will obviously break the layout of the scroll view if it has its own frame within the view.

scrollView.frame = CGRect(
    x: bounds.minX,
    y: bounds.minY,
    width: bounds.width,
    height: messageViewFrame.minY
)

So I changed a few lines in MessageViewController so the scroll view would get its x, y, and width values from its existing frame, and then calculate its height relative to the messageViewFrame

scrollView.frame = CGRect(
    x: scrollView.frame.minX,
    y: scrollView.frame.minY,
    width: scrollView.frame.width,
    height: messageViewFrame.minY - scrollView.frame.minY
)

This way the scroll view gets positioned correctly within its containing view, and there are no other changes to way the message view controller gets setup.

override func viewDidLoad() {
        super.viewDidLoad()

        setup(scrollView: myTableView)

        borderColor = UIColor(red: 0.88, green: 0.88, blue: 0.88, alpha: 0.88)
        messageView.inset = UIEdgeInsets(top: 16, left: 8, bottom: 16, right: 8)
        messageView.layer.borderColor = UIColor.clear.cgColor
        messageView.textView.placeholderText = "Send message"
        messageView.textView.placeholderTextColor = .lightGray
       
        ...
    }

Let me know if that looks good or if there is anything you'd like changed.

Use use existing scrollview frame to calculate new frame values with offset from messageView
@jasonaibrahim jasonaibrahim changed the title Update MessageViewController.swift Calculate scroll view frame correctly Aug 27, 2018
@rnystrom
Copy link
Member

Love this! I think you’re totally right. Of I need to get some unit and UI tests in this lib...

Can you change one thing: since we’re accessing the frame prop a few times, put it in a local variable and use that instead? Pet peeve of mine.

let frame = scrollView.frame
// use frame instead

Sent with GitHawk

@jasonaibrahim
Copy link
Contributor Author

hi @rnystrom changes made as requested.

on a separate note, ive found some minor issues with the current behavior of the scrollview. basically, the scrollview will jump to the middle any time the user starts typing. ive seen in some cases too when on a physical device, the scrollview will nudge upwards slightly with every keystroke (i cant reproduce this anymore).

ive boiled this down to the calls being made to view.setNeedsLayout(). It seems like every time this is called, the calculation being made in MessageViewController#layout() is off. I spent hours trying to debug - the computation seems correct when it hits the breakpoints but the actual behavior indicates otherwise. the only thing I could find that would correct the behavior was removing the calls to view.setNeedsLayout()

I've created another branch where I removed this call in MessageViewController#needsLayout and MessageViewController+MessageViewDelegate#wantsLayout and so far the behavior of the scroll view has been improved (as far as I can tell).

If alright with you I will open a separate pull request. I've also added an additional feature to that branch where the scroll view will automatically scroll to the bottom when the keyboard is revealed if a new instance variable scrollsToBottomOnKeyboardReveal is set.

@rnystrom rnystrom merged commit 5d30ee4 into GitHawkApp:master Sep 22, 2018
rnystrom added a commit that referenced this pull request Oct 14, 2018
rnystrom added a commit that referenced this pull request Oct 14, 2018
* Revert "Stick the offset when height changes (#81)"

This reverts commit 41e1a67.

* Revert "Calculate scroll view frame correctly (#72)"

This reverts commit 5d30ee4.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants