Swift - How to apply the drop shadow only on UIButton Bottom Shadow but not for its image and its title...
up vote
0
down vote
favorite
I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
And here is what I want to have:
This is my current code:
button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false
Please help. Thanks in advance.
ios swift uibutton uikit
add a comment |
up vote
0
down vote
favorite
I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
And here is what I want to have:
This is my current code:
button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false
Please help. Thanks in advance.
ios swift uibutton uikit
1
It looks like your button has transparent background, changing it's background to the white should do the trick.
– Fahri Azimov
Nov 5 at 3:43
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
And here is what I want to have:
This is my current code:
button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false
Please help. Thanks in advance.
ios swift uibutton uikit
I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
And here is what I want to have:
This is my current code:
button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false
Please help. Thanks in advance.
ios swift uibutton uikit
ios swift uibutton uikit
asked Nov 5 at 3:40
Va Visal
744816
744816
1
It looks like your button has transparent background, changing it's background to the white should do the trick.
– Fahri Azimov
Nov 5 at 3:43
add a comment |
1
It looks like your button has transparent background, changing it's background to the white should do the trick.
– Fahri Azimov
Nov 5 at 3:43
1
1
It looks like your button has transparent background, changing it's background to the white should do the trick.
– Fahri Azimov
Nov 5 at 3:43
It looks like your button has transparent background, changing it's background to the white should do the trick.
– Fahri Azimov
Nov 5 at 3:43
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.
Like I have done below:
yourButtonView.layer.borderWidth = 0.5
yourButtonView.layer.borderColor = UIColor.gray.cgColor
yourButtonView.layer.shadowColor = UIColor.black.cgColor
yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)
yourButtonView.layer.shadowOpacity = 1.0
yourButtonView.layer.shadowRadius = 0
yourButtonView.layer.masksToBounds = false
add a comment |
up vote
0
down vote
Please use the below extension for creating shadow
extension UIView {
func addshadow(top: Bool,
left: Bool,
bottom: Bool,
right: Bool
) {
let shadowRadius: CGFloat = 2.0
self.layer.masksToBounds = false
self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.layer.shadowRadius = shadowRadius
self.layer.shadowOpacity = 0.3
let path = UIBezierPath()
var x: CGFloat = 0
var y: CGFloat = 0
var viewWidth = self.frame.width
var viewHeight = self.frame.height
if (!top) {
y+=(shadowRadius+1)
}
if (!bottom) {
viewHeight-=(shadowRadius+1)
}
if (!left) {
x+=(shadowRadius+1)
}
if (!right) {
viewWidth-=(shadowRadius+1)
}
path.move(to: CGPoint(x: x, y: y))
path.addLine(to: CGPoint(x: x, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: y))
path.close()
self.layer.shadowPath = path.cgPath
}
}
use -
button.addshadow(top: false, left: false, bottom: true, right: false)
add a comment |
up vote
0
down vote
Checkout below lines of code
btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)
btn.imageView?.layer.shadowColor = UIColor.blue.cgColor
btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.imageView?.layer.shadowOpacity = 1.0
btn.imageView?.layer.shadowRadius = 5
btn.imageView?.layer.masksToBounds = false
btn.setTitle(" hello", for: .normal)
btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor
btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.titleLabel?.layer.shadowOpacity = 1.0
btn.titleLabel?.layer.shadowRadius = 3
btn.titleLabel?.layer.masksToBounds = false
btn.backgroundColor = UIColor.red
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.
Like I have done below:
yourButtonView.layer.borderWidth = 0.5
yourButtonView.layer.borderColor = UIColor.gray.cgColor
yourButtonView.layer.shadowColor = UIColor.black.cgColor
yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)
yourButtonView.layer.shadowOpacity = 1.0
yourButtonView.layer.shadowRadius = 0
yourButtonView.layer.masksToBounds = false
add a comment |
up vote
0
down vote
Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.
Like I have done below:
yourButtonView.layer.borderWidth = 0.5
yourButtonView.layer.borderColor = UIColor.gray.cgColor
yourButtonView.layer.shadowColor = UIColor.black.cgColor
yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)
yourButtonView.layer.shadowOpacity = 1.0
yourButtonView.layer.shadowRadius = 0
yourButtonView.layer.masksToBounds = false
add a comment |
up vote
0
down vote
up vote
0
down vote
Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.
Like I have done below:
yourButtonView.layer.borderWidth = 0.5
yourButtonView.layer.borderColor = UIColor.gray.cgColor
yourButtonView.layer.shadowColor = UIColor.black.cgColor
yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)
yourButtonView.layer.shadowOpacity = 1.0
yourButtonView.layer.shadowRadius = 0
yourButtonView.layer.masksToBounds = false
Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.
Like I have done below:
yourButtonView.layer.borderWidth = 0.5
yourButtonView.layer.borderColor = UIColor.gray.cgColor
yourButtonView.layer.shadowColor = UIColor.black.cgColor
yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)
yourButtonView.layer.shadowOpacity = 1.0
yourButtonView.layer.shadowRadius = 0
yourButtonView.layer.masksToBounds = false
edited Nov 5 at 4:56
D V
29819
29819
answered Nov 5 at 3:57
Ahemadabbas Vagh
395
395
add a comment |
add a comment |
up vote
0
down vote
Please use the below extension for creating shadow
extension UIView {
func addshadow(top: Bool,
left: Bool,
bottom: Bool,
right: Bool
) {
let shadowRadius: CGFloat = 2.0
self.layer.masksToBounds = false
self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.layer.shadowRadius = shadowRadius
self.layer.shadowOpacity = 0.3
let path = UIBezierPath()
var x: CGFloat = 0
var y: CGFloat = 0
var viewWidth = self.frame.width
var viewHeight = self.frame.height
if (!top) {
y+=(shadowRadius+1)
}
if (!bottom) {
viewHeight-=(shadowRadius+1)
}
if (!left) {
x+=(shadowRadius+1)
}
if (!right) {
viewWidth-=(shadowRadius+1)
}
path.move(to: CGPoint(x: x, y: y))
path.addLine(to: CGPoint(x: x, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: y))
path.close()
self.layer.shadowPath = path.cgPath
}
}
use -
button.addshadow(top: false, left: false, bottom: true, right: false)
add a comment |
up vote
0
down vote
Please use the below extension for creating shadow
extension UIView {
func addshadow(top: Bool,
left: Bool,
bottom: Bool,
right: Bool
) {
let shadowRadius: CGFloat = 2.0
self.layer.masksToBounds = false
self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.layer.shadowRadius = shadowRadius
self.layer.shadowOpacity = 0.3
let path = UIBezierPath()
var x: CGFloat = 0
var y: CGFloat = 0
var viewWidth = self.frame.width
var viewHeight = self.frame.height
if (!top) {
y+=(shadowRadius+1)
}
if (!bottom) {
viewHeight-=(shadowRadius+1)
}
if (!left) {
x+=(shadowRadius+1)
}
if (!right) {
viewWidth-=(shadowRadius+1)
}
path.move(to: CGPoint(x: x, y: y))
path.addLine(to: CGPoint(x: x, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: y))
path.close()
self.layer.shadowPath = path.cgPath
}
}
use -
button.addshadow(top: false, left: false, bottom: true, right: false)
add a comment |
up vote
0
down vote
up vote
0
down vote
Please use the below extension for creating shadow
extension UIView {
func addshadow(top: Bool,
left: Bool,
bottom: Bool,
right: Bool
) {
let shadowRadius: CGFloat = 2.0
self.layer.masksToBounds = false
self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.layer.shadowRadius = shadowRadius
self.layer.shadowOpacity = 0.3
let path = UIBezierPath()
var x: CGFloat = 0
var y: CGFloat = 0
var viewWidth = self.frame.width
var viewHeight = self.frame.height
if (!top) {
y+=(shadowRadius+1)
}
if (!bottom) {
viewHeight-=(shadowRadius+1)
}
if (!left) {
x+=(shadowRadius+1)
}
if (!right) {
viewWidth-=(shadowRadius+1)
}
path.move(to: CGPoint(x: x, y: y))
path.addLine(to: CGPoint(x: x, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: y))
path.close()
self.layer.shadowPath = path.cgPath
}
}
use -
button.addshadow(top: false, left: false, bottom: true, right: false)
Please use the below extension for creating shadow
extension UIView {
func addshadow(top: Bool,
left: Bool,
bottom: Bool,
right: Bool
) {
let shadowRadius: CGFloat = 2.0
self.layer.masksToBounds = false
self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.layer.shadowRadius = shadowRadius
self.layer.shadowOpacity = 0.3
let path = UIBezierPath()
var x: CGFloat = 0
var y: CGFloat = 0
var viewWidth = self.frame.width
var viewHeight = self.frame.height
if (!top) {
y+=(shadowRadius+1)
}
if (!bottom) {
viewHeight-=(shadowRadius+1)
}
if (!left) {
x+=(shadowRadius+1)
}
if (!right) {
viewWidth-=(shadowRadius+1)
}
path.move(to: CGPoint(x: x, y: y))
path.addLine(to: CGPoint(x: x, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: y))
path.close()
self.layer.shadowPath = path.cgPath
}
}
use -
button.addshadow(top: false, left: false, bottom: true, right: false)
answered Nov 5 at 5:04
Abhinav Jha
12410
12410
add a comment |
add a comment |
up vote
0
down vote
Checkout below lines of code
btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)
btn.imageView?.layer.shadowColor = UIColor.blue.cgColor
btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.imageView?.layer.shadowOpacity = 1.0
btn.imageView?.layer.shadowRadius = 5
btn.imageView?.layer.masksToBounds = false
btn.setTitle(" hello", for: .normal)
btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor
btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.titleLabel?.layer.shadowOpacity = 1.0
btn.titleLabel?.layer.shadowRadius = 3
btn.titleLabel?.layer.masksToBounds = false
btn.backgroundColor = UIColor.red
add a comment |
up vote
0
down vote
Checkout below lines of code
btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)
btn.imageView?.layer.shadowColor = UIColor.blue.cgColor
btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.imageView?.layer.shadowOpacity = 1.0
btn.imageView?.layer.shadowRadius = 5
btn.imageView?.layer.masksToBounds = false
btn.setTitle(" hello", for: .normal)
btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor
btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.titleLabel?.layer.shadowOpacity = 1.0
btn.titleLabel?.layer.shadowRadius = 3
btn.titleLabel?.layer.masksToBounds = false
btn.backgroundColor = UIColor.red
add a comment |
up vote
0
down vote
up vote
0
down vote
Checkout below lines of code
btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)
btn.imageView?.layer.shadowColor = UIColor.blue.cgColor
btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.imageView?.layer.shadowOpacity = 1.0
btn.imageView?.layer.shadowRadius = 5
btn.imageView?.layer.masksToBounds = false
btn.setTitle(" hello", for: .normal)
btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor
btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.titleLabel?.layer.shadowOpacity = 1.0
btn.titleLabel?.layer.shadowRadius = 3
btn.titleLabel?.layer.masksToBounds = false
btn.backgroundColor = UIColor.red
Checkout below lines of code
btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)
btn.imageView?.layer.shadowColor = UIColor.blue.cgColor
btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.imageView?.layer.shadowOpacity = 1.0
btn.imageView?.layer.shadowRadius = 5
btn.imageView?.layer.masksToBounds = false
btn.setTitle(" hello", for: .normal)
btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor
btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.titleLabel?.layer.shadowOpacity = 1.0
btn.titleLabel?.layer.shadowRadius = 3
btn.titleLabel?.layer.masksToBounds = false
btn.backgroundColor = UIColor.red
edited Nov 5 at 5:58
answered Nov 5 at 5:40
Sakshi
762819
762819
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148010%2fswift-how-to-apply-the-drop-shadow-only-on-uibutton-bottom-shadow-but-not-for%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
It looks like your button has transparent background, changing it's background to the white should do the trick.
– Fahri Azimov
Nov 5 at 3:43