Title not showing on UIButton Gradient View

Multi tool use
Multi tool use












0














I have a gradient class attached to my UIButton and the gradient shows fine. However, the title or text of the UIButton is not showing. It seems the gradient is covering the text. Heres my code:



@IBDesignable
class GradientView: UIButton {

@IBInspectable var topColor: UIColor = #colorLiteral(red: 1, green: 0.0318463631, blue: 0, alpha: 1) {
didSet {
self.setNeedsLayout()
}
}

@IBInspectable var bottomColor: UIColor = #colorLiteral(red: 1, green: 0.4265971184, blue: 0, alpha: 1) {
didSet {
self.setNeedsLayout()
}
}

override func layoutSubviews() {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
gradientLayer.frame = self.bounds
gradientLayer.cornerRadius = layer.cornerRadius
self.layer.insertSublayer(gradientLayer, at: 0)
}
//Rounded Button Functions
@IBInspectable var cornerRadius : CGFloat = 3.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}

override func awakeFromNib() {
self.setupView()
}

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupView()
}


func setupView() {
self.layer.cornerRadius = cornerRadius
}

@IBInspectable var border : CGFloat = 1.0 {
didSet {
self.layer.borderWidth = border
}
}

@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}


I don't know what the problem is. I used this code for another project and it showed fine.
Thanks










share|improve this question




















  • 1




    try to add super.layoutSubviews() in layoutSubviews method. I think it is a problem.
    – Andrew Romanov
    Dec 28 '18 at 3:17










  • While I tried to replicate the issue, I have tried Andrew Romanov's suggestion. It is working!
    – Rurouni
    Dec 28 '18 at 3:21
















0














I have a gradient class attached to my UIButton and the gradient shows fine. However, the title or text of the UIButton is not showing. It seems the gradient is covering the text. Heres my code:



@IBDesignable
class GradientView: UIButton {

@IBInspectable var topColor: UIColor = #colorLiteral(red: 1, green: 0.0318463631, blue: 0, alpha: 1) {
didSet {
self.setNeedsLayout()
}
}

@IBInspectable var bottomColor: UIColor = #colorLiteral(red: 1, green: 0.4265971184, blue: 0, alpha: 1) {
didSet {
self.setNeedsLayout()
}
}

override func layoutSubviews() {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
gradientLayer.frame = self.bounds
gradientLayer.cornerRadius = layer.cornerRadius
self.layer.insertSublayer(gradientLayer, at: 0)
}
//Rounded Button Functions
@IBInspectable var cornerRadius : CGFloat = 3.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}

override func awakeFromNib() {
self.setupView()
}

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupView()
}


func setupView() {
self.layer.cornerRadius = cornerRadius
}

@IBInspectable var border : CGFloat = 1.0 {
didSet {
self.layer.borderWidth = border
}
}

@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}


I don't know what the problem is. I used this code for another project and it showed fine.
Thanks










share|improve this question




















  • 1




    try to add super.layoutSubviews() in layoutSubviews method. I think it is a problem.
    – Andrew Romanov
    Dec 28 '18 at 3:17










  • While I tried to replicate the issue, I have tried Andrew Romanov's suggestion. It is working!
    – Rurouni
    Dec 28 '18 at 3:21














0












0








0







I have a gradient class attached to my UIButton and the gradient shows fine. However, the title or text of the UIButton is not showing. It seems the gradient is covering the text. Heres my code:



@IBDesignable
class GradientView: UIButton {

@IBInspectable var topColor: UIColor = #colorLiteral(red: 1, green: 0.0318463631, blue: 0, alpha: 1) {
didSet {
self.setNeedsLayout()
}
}

@IBInspectable var bottomColor: UIColor = #colorLiteral(red: 1, green: 0.4265971184, blue: 0, alpha: 1) {
didSet {
self.setNeedsLayout()
}
}

override func layoutSubviews() {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
gradientLayer.frame = self.bounds
gradientLayer.cornerRadius = layer.cornerRadius
self.layer.insertSublayer(gradientLayer, at: 0)
}
//Rounded Button Functions
@IBInspectable var cornerRadius : CGFloat = 3.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}

override func awakeFromNib() {
self.setupView()
}

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupView()
}


func setupView() {
self.layer.cornerRadius = cornerRadius
}

@IBInspectable var border : CGFloat = 1.0 {
didSet {
self.layer.borderWidth = border
}
}

@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}


I don't know what the problem is. I used this code for another project and it showed fine.
Thanks










share|improve this question















I have a gradient class attached to my UIButton and the gradient shows fine. However, the title or text of the UIButton is not showing. It seems the gradient is covering the text. Heres my code:



@IBDesignable
class GradientView: UIButton {

@IBInspectable var topColor: UIColor = #colorLiteral(red: 1, green: 0.0318463631, blue: 0, alpha: 1) {
didSet {
self.setNeedsLayout()
}
}

@IBInspectable var bottomColor: UIColor = #colorLiteral(red: 1, green: 0.4265971184, blue: 0, alpha: 1) {
didSet {
self.setNeedsLayout()
}
}

override func layoutSubviews() {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
gradientLayer.frame = self.bounds
gradientLayer.cornerRadius = layer.cornerRadius
self.layer.insertSublayer(gradientLayer, at: 0)
}
//Rounded Button Functions
@IBInspectable var cornerRadius : CGFloat = 3.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}

override func awakeFromNib() {
self.setupView()
}

override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupView()
}


func setupView() {
self.layer.cornerRadius = cornerRadius
}

@IBInspectable var border : CGFloat = 1.0 {
didSet {
self.layer.borderWidth = border
}
}

@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}


I don't know what the problem is. I used this code for another project and it showed fine.
Thanks







ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 7:18









Callum

30314




30314










asked Dec 28 '18 at 3:09









Etiekyed

175




175








  • 1




    try to add super.layoutSubviews() in layoutSubviews method. I think it is a problem.
    – Andrew Romanov
    Dec 28 '18 at 3:17










  • While I tried to replicate the issue, I have tried Andrew Romanov's suggestion. It is working!
    – Rurouni
    Dec 28 '18 at 3:21














  • 1




    try to add super.layoutSubviews() in layoutSubviews method. I think it is a problem.
    – Andrew Romanov
    Dec 28 '18 at 3:17










  • While I tried to replicate the issue, I have tried Andrew Romanov's suggestion. It is working!
    – Rurouni
    Dec 28 '18 at 3:21








1




1




try to add super.layoutSubviews() in layoutSubviews method. I think it is a problem.
– Andrew Romanov
Dec 28 '18 at 3:17




try to add super.layoutSubviews() in layoutSubviews method. I think it is a problem.
– Andrew Romanov
Dec 28 '18 at 3:17












While I tried to replicate the issue, I have tried Andrew Romanov's suggestion. It is working!
– Rurouni
Dec 28 '18 at 3:21




While I tried to replicate the issue, I have tried Andrew Romanov's suggestion. It is working!
– Rurouni
Dec 28 '18 at 3:21












1 Answer
1






active

oldest

votes


















2














You forgot to call super.layoutSubviews() in the layoutSubviews().



override func layoutSubviews() {
super.layoutSubviews()
// your code
}





share|improve this answer























    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',
    autoActivateHeartbeat: false,
    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953261%2ftitle-not-showing-on-uibutton-gradient-view%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You forgot to call super.layoutSubviews() in the layoutSubviews().



    override func layoutSubviews() {
    super.layoutSubviews()
    // your code
    }





    share|improve this answer




























      2














      You forgot to call super.layoutSubviews() in the layoutSubviews().



      override func layoutSubviews() {
      super.layoutSubviews()
      // your code
      }





      share|improve this answer


























        2












        2








        2






        You forgot to call super.layoutSubviews() in the layoutSubviews().



        override func layoutSubviews() {
        super.layoutSubviews()
        // your code
        }





        share|improve this answer














        You forgot to call super.layoutSubviews() in the layoutSubviews().



        override func layoutSubviews() {
        super.layoutSubviews()
        // your code
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 28 '18 at 5:44









        SPatel

        1,459926




        1,459926










        answered Dec 28 '18 at 3:28









        Andrew Romanov

        2,43021630




        2,43021630






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953261%2ftitle-not-showing-on-uibutton-gradient-view%23new-answer', 'question_page');
            }
            );

            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







            scRe,T7jRKO2,in 7iCJMIAibzyPJ75
            YA1ngcWkyRjV nJJFon0OS0ZwK9u97Y6,Nh2A,GY F DYuF,BqU8FFGRinHTyd RnJ5 xXtcp9co

            Popular posts from this blog

            Monofisismo

            compose and upload a new article using a custom form

            “attempting to read past stream EOM” using Sybase.AdoNet4.AseClient