简体   繁体   English

Swift中UIScrollView内的UITableView高度

[英]UITableView height inside UIScrollView in Swift

I'm trying to add a UITableView inside UIScrollView but when I set the height of UITableView using tableView.contentSize.height it is not setting the height properly (It add extra height to whats actual height is) 我正在尝试在UIScrollView添加UITableView ,但是当我使用tableView.contentSize.height设置UITableView的高度时,它没有正确设置高度(它为实际高度增加了额外的高度)

Can anyone Please help? 谁能帮忙吗?

这就是我要实现的

I am able to make the content size of table to set as table height and modify the content size of scrollView by following way. 我可以将表格的内容大小设置为表格高度,并通过以下方式修改scrollView的内容大小。

I just designed the table inside the scrollView and call to modify the height of table and content size of scrollView from viewDidAppear 我只是设计了表内滚动视图,并呼吁修改从滚动视图的表和内容的高度尺寸viewDidAppear

My Code : 我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.extraDesignAfterStoryboard();
}

override func viewDidAppear(animated: Bool) {

    self.modifyContentHeight();
}

func extraDesignAfterStoryboard()
{
    scrolVw = UIScrollView();
    scrolVw.frame = CGRectMake(0, 64, self.view.frame.size.width, 200);
    scrolVw.backgroundColor = UIColor.greenColor();
    self.view.addSubview(scrolVw);

    tblViewScr = UITableView(frame: CGRectMake(0, 80, scrolVw.frame.size.width, 120), style: UITableViewStyle.Plain);
    tblViewScr.delegate = self;
    tblViewScr.tag = 100;
    tblViewScr.dataSource = self;
    tblViewScr.scrollEnabled = false;
    tblViewScr.backgroundColor = UIColor.redColor();
    scrolVw.addSubview(tblViewScr);
}

func modifyContentHeight()
{
    if(self.tblViewScr.contentSize.height > self.tblViewScr.frame.height){
        var frame: CGRect = self.tblViewScr.frame;
        frame.size.height = self.tblViewScr.contentSize.height;
        self.tblViewScr.frame = frame;

        var contentSize = self.scrolVw.contentSize;
        contentSize.height = CGRectGetMaxY(self.tblViewScr.frame);
        self.scrolVw.contentSize = contentSize;
    }
}

It is working here in my Demo project. 它在我的演示项目中正在运行。 Can you check once, If it works for you. 您能否检查一次,如果它适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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