简体   繁体   中英

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)

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.

I just designed the table inside the scrollView and call to modify the height of table and content size of scrollView from 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.

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