简体   繁体   中英

UITableView Cell should not scroll horizontally swift iOS

I have a table view which has card kind of cells as shown in picture. I have set the content Inset so that I can get the cells with 20px spacing in left,right and top. 在此输入图像描述 tableVw.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 0, right: 20)

Its displaying as I expected but the problem is, cells can be moved in all direction. But when I move it to right/top, it automatically comes back to original position. But when I move to left it just goes inside and don't scroll back to original position as shown in picture. 在此输入图像描述

I don't want the cells to move at all or I want the cells to come back to centre if its dragged anywhere also. Please help!

不要在tableview中提供uiedgeinsets,而是在uitableview单元格中添加一个视图,覆盖整个单元格,并在该视图中添加另一个uiview,并从顶部底部前导尾部等于8或任何你想要的约束,然后单元格不会移动和你的tableview单元格看起来它有edgeinsets。

If you're using AutoLayout, by setting this only should work for you:

In code:

tableView.alwaysBounceVertical = false

or In Interface Builder: Just find this option and untick "Bounce Vertically" option.

Here's the reference:

在此输入图像描述

If you're not using AutoLayout:

override func viewDidLayoutSubviews() {
    // Enable scrolling based on content height
    tableView.isScrollEnabled = tableView.contentSize.height > tableView.frame.size.height
 }

and also try clipToBounds

tableview.clipsToBounds = true

您需要将clipsToBounds属性设置为true

tableview.clipsToBounds = true

I achieved what I wanted by below code.

postsTable.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 0, right: -20)

And in UITableViewCell class :

override var frame: CGRect{
    get {
        return super.frame
    }
    set(newFrame){
        var frame = newFrame
        frame.size.width = kScreenWidth - 40
        super.frame = frame
    }
}

Now if I drag the cell left or right also its coming back to original position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM