2017-08-14 104 views
0

嗨我想在UITextField下得到陰影。我已經對UIView進行了如下擴展。但是,如果切斷TextField的一半用戶界面。uiview下沒有陰影

如何在UIView/UITextField下添加陰影?

public extension UIView { 
    func installShadow() { 
     self.layer.masksToBounds = false 
     self.layer.backgroundColor = UIColor.white.cgColor 
     self.layer.shadowColor = UIColor.lightGray.cgColor 
     self.layer.shadowRadius = 5 
     self.layer.shadowOpacity = 1 
     self.layer.shadowOffset = CGSize(width: 0, height: 1) 

     let shadowPath = CGPath(ellipseIn: CGRect(x: self.frame.origin.x, 
                y: self.frame.origin.y, 
                width: self.frame.size.width, 
                height: self.frame.size.height), 
           transform: nil) 

     self.layer.shadowPath = shadowPath 
    } 
} 
+0

您正在使用'CGPath(ellipseIn:'這是故意的嗎? –

+0

試試這個'self.layer.shadowPath = UIBezierPath(rect:self.bounds).cgPath' –

+0

你需要顯示一個圖像,你希望看到結果。 – DonMag

回答

0

你有2個問題主要是,使用框架,而不是邊界,並2.使用CGPath(ellipseIn:你的道路將是一個橢圓形

使用此代碼

public extension UIView { 
    func installShadow() { 
     self.layer.masksToBounds = false 
     self.layer.backgroundColor = UIColor.white.cgColor 
     self.layer.shadowColor = UIColor.lightGray.cgColor 
     self.layer.shadowRadius = 5 
     self.layer.shadowOpacity = 1 
     self.layer.shadowOffset = CGSize(width: 0, height: 1) 
     self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath 
    } 
} 

希望這對你有所幫助

+0

@ChrisG你可以upvote我的答案? –

0
Try these ... 

public extension UIView { 
    func installShadow() { 
let shadowSize : CGFloat = 0.5 
layer.clipsToBounds = true 
      let shadowPath = UIBezierPath(rect: CGRect(x: -shadowSize/2, 
                 y: -shadowSize/2, 
                 width: subView!.frame.size.width + shadowSize, 
                 height: subView!.frame.size.height + shadowSize)) 
      self.layer.masksToBounds = false 
      self.layer.shadowColor = UIColor.darkGray.cgColor 
      self.layer.shadowOffset = CGSize(width: 0, height: 0) 
      self.layer.shadowOpacity = 0.2 
      self.layer.shadowRadius = 5.0 
      self.layer.cornerRadius = 5.0 
      self.layer.shadowPath = shadowPath.cgPath 
} 
}