When I add a shadow to my table view, it only shows if I have masksToBound = false
as in picture 2. But it will look glitchy. Otherwise it looks fine scrolling up and down as in picture 1 but there's no shadow because I don't set it to false. Changing clipToBounds won't help either. My tableView changes height based on the input into the textField so I think adding a container view might be tricky.
How do I fix this? Is there a simple solution?
@IBOutlet weak var txtSearchBar: UITextField!
@IBOutlet weak var tblList: UITableView!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var shadowView: UIView!
@IBOutlet weak var tblHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var shadowHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var collectionView: UICollectionView!
var majors:[String] = Array()
var results: [String] = Array()
var label = UILabel(frame: CGRect.zero)
var originalMajorsList = ["BIO 1AL", "BIO 1B", "BIO 1A", "MCB 118","..."]
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.collectionViewLayout = columnLayout
tblList.delegate = self
tblList.dataSource = self
txtSearchBar.delegate = self
txtSearchBar.addTarget(self, action: #selector(searchRecords(_ :)), for: .editingChanged)
txtSearchBar.layer.borderColor = UIColor.groupTableViewBackground.cgColor
txtSearchBar.layer.borderWidth = 1
txtSearchBar.layer.cornerRadius = 5
//**glitchy if masksToBounds = false**
tblList.layer.borderColor = UIColor.groupTableViewBackground.cgColor
tblList.layer.borderWidth = 1
tblList.layer.cornerRadius = 5
tblList.layer.shadowColor = UIColor.lightGray.cgColor
tblList.layer.shadowOffset = CGSize(width: -10, height: 10)
tblList.layer.shadowOpacity = 0.5
tblList.layer.shadowRadius = 50
tblList.layer.masksToBounds = false
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
txtSearchBar.resignFirstResponder()
return true
}
//MARK:- searchRecords
@objc func searchRecords(_ textField: UITextField) {
tblList.isHidden = false
self.majors.removeAll()
if textField.text?.count != 0 {
for major in originalMajorsList {
if let majorToSearch = textField.text{
let range = major.lowercased().range(of: majorToSearch, options: .caseInsensitive, range: nil, locale: nil)
if range != nil {
self.majors.append(major)
}
}
}
} else {
tblHeightConstraint.constant = 0
}
tblList.reloadData()
}
override func viewDidLayoutSubviews(){
tblHeightConstraint.constant = max(0, min(UIScreen.main.bounds.height * 0.4, tblList.contentSize.height))
}
//MARK:- UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return majors.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "major")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "major")
}
cell?.textLabel?.text = majors[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if !results.contains(majors[indexPath.row]) {
results.append(majors[indexPath.row])
}
textField.text = ""
tableView.isHidden = true
collectionView.reloadData()
}