简体   繁体   中英

How do I increase the gap between a UITableView section headerview and first cell?

I added a UISegmentedControl as my headerview for a section. Now the top part of my first first tableView cell is being cut off as shown in image.

在此处输入图片说明

Is there a way to fix this?

I've try messing with tableView insets and scrollView but had zero luck.

In my viewDidLayoutSubviews I cancelling out the height fixes it:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    var frame = segmentedControl.frame
    frame.size.height = 29.0
    segmentedControl.frame = frame
    segmentedControl.tintColor = ColourHelper().segmentedControlRed
    segmentedControl.backgroundColor = UIColor.whiteColor()
    segmentedControl.selectedSegmentIndex = 1
    segmentedControl.addTarget(self, action: "segmentedControlTapped:", forControlEvents: UIControlEvents.ValueChanged)
    tableView.headerViewForSection(1)?.hidden = true
}

But now my UISegmentedControl's height doesn't match the rest of my segmented controls in my app.

在此处输入图片说明

I didn't need to set the frame manually like shown in my question. I removed that code:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    segmentedControl.tintColor = ColourHelper().segmentedControlRed
    segmentedControl.backgroundColor = UIColor.whiteColor()
    segmentedControl.selectedSegmentIndex = 1
    segmentedControl.addTarget(self, action: "segmentedControlTapped:", forControlEvents: UIControlEvents.ValueChanged)
    tableView.headerViewForSection(1)?.removeFromSuperview()

}

Then set the height of the header like so:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 29.0
}

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