2017-08-14 11 views
0

こんにちは私はUITextFieldの下に影をつけようとしています。私は、UIViewに続く拡張を行った。しかし、TextFieldの場合は、UIの半分を切り捨てます。uiviewの下に影がありません

どのようにUIView/UITextFieldの下に影を追加しますか? 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) 

     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つの問題を抱えている、とが楕円になります

このコード

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 
} 
} 
関連する問題