简体   繁体   中英

Shadow from UITableViewCell gets cut by UINavigationbar

I have the following code for an UIView that gets added via cell.contentView.addSubview(insets) to each cell in a UITableView:

import TinyConstraints


let insets = UIView()
insets.backgroundColor = .clear
let background = UIView()
background.backgroundColor = .white
background.layer.cornerRadius = 20
background.layer.shadowColor = UIColor.gray.cgColor
background.layer.shadowRadius = 16
background.layer.shadowOffset = CGSize(width: 0, height: 0)
background.layer.shadowOpacity = 0.5
/*
 More content like labels etc added to background
*/
insets.addSubview(background)
background.edgesToSuperview(insets: .horizontal(16) + .vertical(12))

This cells get added in a UITableViewController which is inside a UINavigationController. The Problem is that the top of the shadow of the first cell gets cut off by the navigation bar:

在此处输入图像描述

I could, of course, lower the shadowRadius and/or increase the offset of the first cell at the top. But that increased space between "Home" and the first cell wouldn't look good, neither would a smaller shadow radius.

So I'm wondering, is there a way to let the shadow "blend into" the navigation bar?

Thanks in advance!

One thing you can do is completely remove the UINavigationBar from this ViewController and create your own custom one by using a tableHeaderView : https://developer.apple.com/documentation/uikit/uitableview/1614904-tableheaderview . This way the shadow will blend into the header

Note: you can still keep the UINavigationController , just simply remove the bar itself on the home screen.

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