CAGradientLayer does not display
up vote
0
down vote
favorite
I have looked over a bunch of other answers to questions like this, but none of the answers seem to solve my problem, so looking for a little help.
I'm trying to apply a vertical gradient to a UIButton, but the gradient layer is just not showing up in my view. Here is the relevant code:
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
addButton.title = "Start counting"
addButton.layer.cornerRadius = 30.0
addButton.layer.borderWidth = 1.0
addButton.layer.borderColor = darkPurple.cgColor
addButton.translatesAutoresizingMaskIntoConstraints = false
myView.addSubview(addButton)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
...and the code to apply the gradient:
typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint)
enum GradientOrientation {
case topRightBottomLeft
case topLeftBottomRight
case horizontal
case vertical
var startPoint: CGPoint {
return points.startPoint
}
var endPoint: CGPoint {
return points.endPoint
}
var points: GradientPoints {
switch self {
case .topRightBottomLeft:
return (CGPoint(x: 0.0, y: 1.0), CGPoint(x: 1.0, y: 0.0))
case .topLeftBottomRight:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 1.0))
case .horizontal:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 0.0))
case .vertical:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 0.0, y: 1.0))
}
}
}
extension UIView {
func applyGradient(with colors: [UIColor], gradientOrientation orientation: GradientOrientation) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colors.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
gradient.borderColor = self.layer.borderColor
gradient.borderWidth = self.layer.borderWidth
gradient.cornerRadius = self.layer.cornerRadius
gradient.masksToBounds = true
gradient.isHidden = false
self.layer.insertSublayer(gradient, at: 0)
}
}
This is what I get:
empty button
Thanks for your help!
ios swift xcode uibutton cagradientlayer
add a comment |
up vote
0
down vote
favorite
I have looked over a bunch of other answers to questions like this, but none of the answers seem to solve my problem, so looking for a little help.
I'm trying to apply a vertical gradient to a UIButton, but the gradient layer is just not showing up in my view. Here is the relevant code:
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
addButton.title = "Start counting"
addButton.layer.cornerRadius = 30.0
addButton.layer.borderWidth = 1.0
addButton.layer.borderColor = darkPurple.cgColor
addButton.translatesAutoresizingMaskIntoConstraints = false
myView.addSubview(addButton)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
...and the code to apply the gradient:
typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint)
enum GradientOrientation {
case topRightBottomLeft
case topLeftBottomRight
case horizontal
case vertical
var startPoint: CGPoint {
return points.startPoint
}
var endPoint: CGPoint {
return points.endPoint
}
var points: GradientPoints {
switch self {
case .topRightBottomLeft:
return (CGPoint(x: 0.0, y: 1.0), CGPoint(x: 1.0, y: 0.0))
case .topLeftBottomRight:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 1.0))
case .horizontal:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 0.0))
case .vertical:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 0.0, y: 1.0))
}
}
}
extension UIView {
func applyGradient(with colors: [UIColor], gradientOrientation orientation: GradientOrientation) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colors.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
gradient.borderColor = self.layer.borderColor
gradient.borderWidth = self.layer.borderWidth
gradient.cornerRadius = self.layer.cornerRadius
gradient.masksToBounds = true
gradient.isHidden = false
self.layer.insertSublayer(gradient, at: 0)
}
}
This is what I get:
empty button
Thanks for your help!
ios swift xcode uibutton cagradientlayer
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have looked over a bunch of other answers to questions like this, but none of the answers seem to solve my problem, so looking for a little help.
I'm trying to apply a vertical gradient to a UIButton, but the gradient layer is just not showing up in my view. Here is the relevant code:
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
addButton.title = "Start counting"
addButton.layer.cornerRadius = 30.0
addButton.layer.borderWidth = 1.0
addButton.layer.borderColor = darkPurple.cgColor
addButton.translatesAutoresizingMaskIntoConstraints = false
myView.addSubview(addButton)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
...and the code to apply the gradient:
typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint)
enum GradientOrientation {
case topRightBottomLeft
case topLeftBottomRight
case horizontal
case vertical
var startPoint: CGPoint {
return points.startPoint
}
var endPoint: CGPoint {
return points.endPoint
}
var points: GradientPoints {
switch self {
case .topRightBottomLeft:
return (CGPoint(x: 0.0, y: 1.0), CGPoint(x: 1.0, y: 0.0))
case .topLeftBottomRight:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 1.0))
case .horizontal:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 0.0))
case .vertical:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 0.0, y: 1.0))
}
}
}
extension UIView {
func applyGradient(with colors: [UIColor], gradientOrientation orientation: GradientOrientation) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colors.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
gradient.borderColor = self.layer.borderColor
gradient.borderWidth = self.layer.borderWidth
gradient.cornerRadius = self.layer.cornerRadius
gradient.masksToBounds = true
gradient.isHidden = false
self.layer.insertSublayer(gradient, at: 0)
}
}
This is what I get:
empty button
Thanks for your help!
ios swift xcode uibutton cagradientlayer
I have looked over a bunch of other answers to questions like this, but none of the answers seem to solve my problem, so looking for a little help.
I'm trying to apply a vertical gradient to a UIButton, but the gradient layer is just not showing up in my view. Here is the relevant code:
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
addButton.title = "Start counting"
addButton.layer.cornerRadius = 30.0
addButton.layer.borderWidth = 1.0
addButton.layer.borderColor = darkPurple.cgColor
addButton.translatesAutoresizingMaskIntoConstraints = false
myView.addSubview(addButton)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
...and the code to apply the gradient:
typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint)
enum GradientOrientation {
case topRightBottomLeft
case topLeftBottomRight
case horizontal
case vertical
var startPoint: CGPoint {
return points.startPoint
}
var endPoint: CGPoint {
return points.endPoint
}
var points: GradientPoints {
switch self {
case .topRightBottomLeft:
return (CGPoint(x: 0.0, y: 1.0), CGPoint(x: 1.0, y: 0.0))
case .topLeftBottomRight:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 1.0))
case .horizontal:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 0.0))
case .vertical:
return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 0.0, y: 1.0))
}
}
}
extension UIView {
func applyGradient(with colors: [UIColor], gradientOrientation orientation: GradientOrientation) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colors.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
gradient.borderColor = self.layer.borderColor
gradient.borderWidth = self.layer.borderWidth
gradient.cornerRadius = self.layer.cornerRadius
gradient.masksToBounds = true
gradient.isHidden = false
self.layer.insertSublayer(gradient, at: 0)
}
}
This is what I get:
empty button
Thanks for your help!
ios swift xcode uibutton cagradientlayer
ios swift xcode uibutton cagradientlayer
edited Nov 20 at 2:28
Duncan C
91.6k13114194
91.6k13114194
asked Nov 20 at 2:05
kyanring
33
33
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Auto Layout doesn't affect the layers of UIView
objects, so the gradient layer is being applied too early, before the frame of the UIButton
has been calculated. When the frame
property of the gradient layer is assigned, the bounds of the button are still equal to CGRect.zero
, and the gradient layer frame is never updated later.
You'll find that if you add your gradient layer after the button frame has been calculated, it works as expected. For example:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
}
Another option would be to update the frame
property of the gradient layer in viewDidLayoutSubviews()
or create a custom UIButton
subclass and update the gradient layer frame whenever the frame of the button updates.
Reference
This worked! Thanks @ryanecrist!
– kyanring
Nov 20 at 3:43
add a comment |
up vote
0
down vote
Sublayers are in back-to-front order. Inserting a layer at index 0 will place it behind any other sublayers. I've never tried to do that with a button, but suspect it has at least 1 sublayer that is covering the gradient you are trying to add.
Try using addSublayer()
instead, which will put the layer on the top of the sublayers array.
Edit:
I built a variant of your code and when I change self.layer.insertSublayer(_:at:)
to addSublayer()
, it works.
You're right about this, for sure. In my case, I needed to make sure the gradient shows up behind the button title, soself.layer.insertSublayer(_:at:)
won't work for me. I did, however, have this backward, thinking that 0 would be the top. Thanks for the correction!
– kyanring
Nov 20 at 5:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385203%2fcagradientlayer-does-not-display%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Auto Layout doesn't affect the layers of UIView
objects, so the gradient layer is being applied too early, before the frame of the UIButton
has been calculated. When the frame
property of the gradient layer is assigned, the bounds of the button are still equal to CGRect.zero
, and the gradient layer frame is never updated later.
You'll find that if you add your gradient layer after the button frame has been calculated, it works as expected. For example:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
}
Another option would be to update the frame
property of the gradient layer in viewDidLayoutSubviews()
or create a custom UIButton
subclass and update the gradient layer frame whenever the frame of the button updates.
Reference
This worked! Thanks @ryanecrist!
– kyanring
Nov 20 at 3:43
add a comment |
up vote
1
down vote
accepted
Auto Layout doesn't affect the layers of UIView
objects, so the gradient layer is being applied too early, before the frame of the UIButton
has been calculated. When the frame
property of the gradient layer is assigned, the bounds of the button are still equal to CGRect.zero
, and the gradient layer frame is never updated later.
You'll find that if you add your gradient layer after the button frame has been calculated, it works as expected. For example:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
}
Another option would be to update the frame
property of the gradient layer in viewDidLayoutSubviews()
or create a custom UIButton
subclass and update the gradient layer frame whenever the frame of the button updates.
Reference
This worked! Thanks @ryanecrist!
– kyanring
Nov 20 at 3:43
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Auto Layout doesn't affect the layers of UIView
objects, so the gradient layer is being applied too early, before the frame of the UIButton
has been calculated. When the frame
property of the gradient layer is assigned, the bounds of the button are still equal to CGRect.zero
, and the gradient layer frame is never updated later.
You'll find that if you add your gradient layer after the button frame has been calculated, it works as expected. For example:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
}
Another option would be to update the frame
property of the gradient layer in viewDidLayoutSubviews()
or create a custom UIButton
subclass and update the gradient layer frame whenever the frame of the button updates.
Reference
Auto Layout doesn't affect the layers of UIView
objects, so the gradient layer is being applied too early, before the frame of the UIButton
has been calculated. When the frame
property of the gradient layer is assigned, the bounds of the button are still equal to CGRect.zero
, and the gradient layer frame is never updated later.
You'll find that if you add your gradient layer after the button frame has been calculated, it works as expected. For example:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
}
Another option would be to update the frame
property of the gradient layer in viewDidLayoutSubviews()
or create a custom UIButton
subclass and update the gradient layer frame whenever the frame of the button updates.
Reference
answered Nov 20 at 2:27
ryanecrist
23828
23828
This worked! Thanks @ryanecrist!
– kyanring
Nov 20 at 3:43
add a comment |
This worked! Thanks @ryanecrist!
– kyanring
Nov 20 at 3:43
This worked! Thanks @ryanecrist!
– kyanring
Nov 20 at 3:43
This worked! Thanks @ryanecrist!
– kyanring
Nov 20 at 3:43
add a comment |
up vote
0
down vote
Sublayers are in back-to-front order. Inserting a layer at index 0 will place it behind any other sublayers. I've never tried to do that with a button, but suspect it has at least 1 sublayer that is covering the gradient you are trying to add.
Try using addSublayer()
instead, which will put the layer on the top of the sublayers array.
Edit:
I built a variant of your code and when I change self.layer.insertSublayer(_:at:)
to addSublayer()
, it works.
You're right about this, for sure. In my case, I needed to make sure the gradient shows up behind the button title, soself.layer.insertSublayer(_:at:)
won't work for me. I did, however, have this backward, thinking that 0 would be the top. Thanks for the correction!
– kyanring
Nov 20 at 5:00
add a comment |
up vote
0
down vote
Sublayers are in back-to-front order. Inserting a layer at index 0 will place it behind any other sublayers. I've never tried to do that with a button, but suspect it has at least 1 sublayer that is covering the gradient you are trying to add.
Try using addSublayer()
instead, which will put the layer on the top of the sublayers array.
Edit:
I built a variant of your code and when I change self.layer.insertSublayer(_:at:)
to addSublayer()
, it works.
You're right about this, for sure. In my case, I needed to make sure the gradient shows up behind the button title, soself.layer.insertSublayer(_:at:)
won't work for me. I did, however, have this backward, thinking that 0 would be the top. Thanks for the correction!
– kyanring
Nov 20 at 5:00
add a comment |
up vote
0
down vote
up vote
0
down vote
Sublayers are in back-to-front order. Inserting a layer at index 0 will place it behind any other sublayers. I've never tried to do that with a button, but suspect it has at least 1 sublayer that is covering the gradient you are trying to add.
Try using addSublayer()
instead, which will put the layer on the top of the sublayers array.
Edit:
I built a variant of your code and when I change self.layer.insertSublayer(_:at:)
to addSublayer()
, it works.
Sublayers are in back-to-front order. Inserting a layer at index 0 will place it behind any other sublayers. I've never tried to do that with a button, but suspect it has at least 1 sublayer that is covering the gradient you are trying to add.
Try using addSublayer()
instead, which will put the layer on the top of the sublayers array.
Edit:
I built a variant of your code and when I change self.layer.insertSublayer(_:at:)
to addSublayer()
, it works.
edited Nov 20 at 2:37
answered Nov 20 at 2:17
Duncan C
91.6k13114194
91.6k13114194
You're right about this, for sure. In my case, I needed to make sure the gradient shows up behind the button title, soself.layer.insertSublayer(_:at:)
won't work for me. I did, however, have this backward, thinking that 0 would be the top. Thanks for the correction!
– kyanring
Nov 20 at 5:00
add a comment |
You're right about this, for sure. In my case, I needed to make sure the gradient shows up behind the button title, soself.layer.insertSublayer(_:at:)
won't work for me. I did, however, have this backward, thinking that 0 would be the top. Thanks for the correction!
– kyanring
Nov 20 at 5:00
You're right about this, for sure. In my case, I needed to make sure the gradient shows up behind the button title, so
self.layer.insertSublayer(_:at:)
won't work for me. I did, however, have this backward, thinking that 0 would be the top. Thanks for the correction!– kyanring
Nov 20 at 5:00
You're right about this, for sure. In my case, I needed to make sure the gradient shows up behind the button title, so
self.layer.insertSublayer(_:at:)
won't work for me. I did, however, have this backward, thinking that 0 would be the top. Thanks for the correction!– kyanring
Nov 20 at 5:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385203%2fcagradientlayer-does-not-display%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown