TableView rounded corners and shadow

前端 未结 4 1042
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 05:35

I have a tableview with three rows. I am trying to make the table rows have rounded corners and also a shadow effect around the entire tableview. For some reason, I cannot m

相关标签:
4条回答
  • 2020-12-08 06:03

    The RDC's answer is good, but for me the result didnt resolve my case, follow is my fix :

    //for table view border
    tableView.layer.borderColor = UIColor .grayColor().CGColor
    tableView.layer.borderWidth = 1.0
    
    //for shadow
    let containerView:UIView = UIView(frame:self.tableView.frame)
    //dont use clear color,fit blue color
    containerView.backgroundColor = UIColor.blueColor()
    //shadow view also need cornerRadius
    containerView.layer.cornerRadius = 10
    containerView.layer.shadowColor = UIColor.lightGrayColor().CGColor
    containerView.layer.shadowOffset = CGSizeMake(-10, 10); //Left-Bottom shadow
    //containerView.layer.shadowOffset = CGSizeMake(10, 10); //Right-Bottom shadow
    containerView.layer.shadowOpacity = 1.0
    containerView.layer.shadowRadius = 2
    
    //for rounded corners
    tableView.layer.cornerRadius = 10
    tableView.layer.masksToBounds = true
    self.view.addSubview(containerView)
    self.view.addSubview(tableView)
    
    0 讨论(0)
  • 2020-12-08 06:06

    Thanks to @beyowulf

    I have upgraded for me with Swift 2.2, to

    • set Border,
    • Rounded corner and
    • Drop shadow to the table view

      //for table view border
      tableView.layer.borderColor = UIColor .grayColor().CGColor
      tableView.layer.borderWidth = 1.0
      
      //for shadow
      let containerView:UIView = UIView(frame:self.tableView.frame)
      containerView.backgroundColor = UIColor.clearColor()
      containerView.layer.shadowColor = UIColor.lightGrayColor().CGColor
      containerView.layer.shadowOffset = CGSizeMake(-10, 10); //Left-Bottom shadow
      //containerView.layer.shadowOffset = CGSizeMake(10, 10); //Right-Bottom shadow
      containerView.layer.shadowOpacity = 1.0
      containerView.layer.shadowRadius = 2
      
      //for rounded corners
      tableView.layer.cornerRadius = 10
      tableView.layer.masksToBounds = true
      self.view.addSubview(containerView)
      containerView.addSubview(tableView)
      

    result looks like

    0 讨论(0)
  • 2020-12-08 06:15

    You can add your table view to a container view and add drop shadow to that container view:

    let containerView:UIView = UIView(frame:CGRect(x: 10, y: 100, width: 300, height: 400))
    self.tableView = UITableView(frame: containerView.bounds), style: .Plain)
    containerView.backgroundColor = UIColor.clearColor()
    containerView.layer.shadowColor = UIColor.darkGrayColor().CGColor   
    containerView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
    containerView.layer.shadowOpacity = 1.0
    containerView.layer.shadowRadius = 2
    // This is for rounded corners
    self.tableView.layer.cornerRadius = 10
    self.tableView.layer.masksToBounds = true
    self.view.addSubview(containerView)
    containerView.addSubview(self.tableView)
    

    Edit

    Swift 3.0:

    let containerView:UIView = UIView(frame:CGRect(x: 10, y: 100, width: 300, height: 400))
    self.tableView = UITableView(frame: containerView.bounds, style: .plain)
    containerView.backgroundColor = UIColor.clear
    containerView.layer.shadowColor = UIColor.darkGray.cgColor
    containerView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
    containerView.layer.shadowOpacity = 1.0
    containerView.layer.shadowRadius = 2
        
    self.tableView.layer.cornerRadius = 10
    self.tableView.layer.masksToBounds = true
    self.view.addSubview(containerView)
    containerView.addSubview(self.tableView)
    

    0 讨论(0)
  • 2020-12-08 06:24

    i tried above solution but in my application tableview didSelectRowAt was not working.

    use this Extension for UITabeleView for corner for shadow

    //if u want shadow for all side use (shadowOffset = .zero)

    extension UITableView {
        func addCorner(){
            self.layer.cornerRadius = 15
            self.clipsToBounds = true
        }
    
        func addShadow(){
            self.layer.shadowColor = UIColor.lightGray.cgColor
            self.layer.shadowRadius = 5
            self.layer.shadowOpacity = 0.5
            self.layer.shadowOffset = .zero
            self.layer.masksToBounds = false
        }
    }
    

    How to use::

       self.tableView.addCorner()
       self.tableView.addShadow()
    
    0 讨论(0)
提交回复
热议问题