UIButton focus in UITableView

1.1k views Asked by At

I am trying to understand how the focus is working when I have button inside Table.

The goal is to create a table view with custom cell, that has the same size as the table view size ( I want to see only one cell at the time ). When the button is getting focused, I want to scroll ( the table view is doing it automatically ), to the next cell and to see only the next cell. I made sure that the button will be in focus and not table view cell, by override this method in the Table cell:

override var canBecomeFocused: Bool{
        return false
}

The button is focused and when I am swipe up/down, the focus is moving between the buttons and not the Table it self ( great success ) ... BUT, the table view is being scrolling automatically, and I want to understand why is some cases ( when the Table and Table Cell are bigger then 360 pixel ) it's scrolling to the next cell and I can see only the next cell ( the table view and the cell in the same size ) , and sometimes ( when the Table and Cell are Smaller then 360 pixel ) the table view scrolling just a little bit, until the next cell is showing only the button ( meaning the cell is not fully shown )

So , I have:

Cell and table view cell size are Greater then 360, we can see that when the button is focused, the table view scrolling to the center of the cell, and we can see only 1 cell:

enter image description here Cell and table view cell size is Smaller then 360, we can see that when the button is focused, the table view scrolling to some point, when the button is shown

enter image description here

FYI: I think the size of the button is not effecting the scroll, only the table view and table view cell size is effect the scrolling.

Some one? any idea ?

This is the test Project: in order to see it better please change the table view size and the cell size to be smaller then 360, and make sure the UIButton is centered: Project

1

There are 1 answers

0
vikas prajapati On

You can put those methods in UITableViewCell's subclass. see

override var preferredFocusEnvironments: [UIFocusEnvironment]{
    // Condition
}
override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
    // Condition
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    // Condition
}

you can see more at this link: enter link description here