How to make an UIView into circle shape in swift 4.2?












0















The width(40) and height(40) of the UIView are equal.



I have tried these but they didn't work for me:



countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2


and also



self.countView.layer.cornerRadius = (countView.frame.size.width)/2


Thanks










share|improve this question

























  • What do you get now?

    – Rakesha Shastri
    Nov 25 '18 at 7:41











  • UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames

    – Mohmmad S
    Nov 25 '18 at 7:55











  • First print countView.frame.size.width and see what is the value.

    – Kamran
    Nov 25 '18 at 8:00
















0















The width(40) and height(40) of the UIView are equal.



I have tried these but they didn't work for me:



countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2


and also



self.countView.layer.cornerRadius = (countView.frame.size.width)/2


Thanks










share|improve this question

























  • What do you get now?

    – Rakesha Shastri
    Nov 25 '18 at 7:41











  • UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames

    – Mohmmad S
    Nov 25 '18 at 7:55











  • First print countView.frame.size.width and see what is the value.

    – Kamran
    Nov 25 '18 at 8:00














0












0








0








The width(40) and height(40) of the UIView are equal.



I have tried these but they didn't work for me:



countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2


and also



self.countView.layer.cornerRadius = (countView.frame.size.width)/2


Thanks










share|improve this question
















The width(40) and height(40) of the UIView are equal.



I have tried these but they didn't work for me:



countView?.clipsToBounds = true
self.countView?.layer.cornerRadius = min((countView?.frame.size.width)!, (countView?.frame.size.height)!)/2


and also



self.countView.layer.cornerRadius = (countView.frame.size.width)/2


Thanks







ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 7:40









Rakesha Shastri

7,39621435




7,39621435










asked Nov 25 '18 at 7:38









B SAI DEERAJB SAI DEERAJ

23




23













  • What do you get now?

    – Rakesha Shastri
    Nov 25 '18 at 7:41











  • UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames

    – Mohmmad S
    Nov 25 '18 at 7:55











  • First print countView.frame.size.width and see what is the value.

    – Kamran
    Nov 25 '18 at 8:00



















  • What do you get now?

    – Rakesha Shastri
    Nov 25 '18 at 7:41











  • UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames

    – Mohmmad S
    Nov 25 '18 at 7:55











  • First print countView.frame.size.width and see what is the value.

    – Kamran
    Nov 25 '18 at 8:00

















What do you get now?

– Rakesha Shastri
Nov 25 '18 at 7:41





What do you get now?

– Rakesha Shastri
Nov 25 '18 at 7:41













UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames

– Mohmmad S
Nov 25 '18 at 7:55





UIView have layer indeed is it shapeable tho, depends on whats inside the bounds of that View, and there constraints and frames

– Mohmmad S
Nov 25 '18 at 7:55













First print countView.frame.size.width and see what is the value.

– Kamran
Nov 25 '18 at 8:00





First print countView.frame.size.width and see what is the value.

– Kamran
Nov 25 '18 at 8:00












2 Answers
2






active

oldest

votes


















0














You can use a mask layer like this:



override func viewDidLoad() {
super.viewDidLoad()

let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
self.countView.layer.mask = maskLayer
}


Example Output



Here the output of a UIView with width/height = 128/128:



example output






share|improve this answer































    3














    In addition to @Stephan answer.



    With less code:



    self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2


    Result:



    img






    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%2f53465559%2fhow-to-make-an-uiview-into-circle-shape-in-swift-4-2%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









      0














      You can use a mask layer like this:



      override func viewDidLoad() {
      super.viewDidLoad()

      let maskLayer = CAShapeLayer()
      maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
      self.countView.layer.mask = maskLayer
      }


      Example Output



      Here the output of a UIView with width/height = 128/128:



      example output






      share|improve this answer




























        0














        You can use a mask layer like this:



        override func viewDidLoad() {
        super.viewDidLoad()

        let maskLayer = CAShapeLayer()
        maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
        self.countView.layer.mask = maskLayer
        }


        Example Output



        Here the output of a UIView with width/height = 128/128:



        example output






        share|improve this answer


























          0












          0








          0







          You can use a mask layer like this:



          override func viewDidLoad() {
          super.viewDidLoad()

          let maskLayer = CAShapeLayer()
          maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
          self.countView.layer.mask = maskLayer
          }


          Example Output



          Here the output of a UIView with width/height = 128/128:



          example output






          share|improve this answer













          You can use a mask layer like this:



          override func viewDidLoad() {
          super.viewDidLoad()

          let maskLayer = CAShapeLayer()
          maskLayer.path = UIBezierPath(ovalIn: self.countView.bounds).cgPath;
          self.countView.layer.mask = maskLayer
          }


          Example Output



          Here the output of a UIView with width/height = 128/128:



          example output







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 8:08









          Stephan SchlechtStephan Schlecht

          5,0151911




          5,0151911

























              3














              In addition to @Stephan answer.



              With less code:



              self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2


              Result:



              img






              share|improve this answer




























                3














                In addition to @Stephan answer.



                With less code:



                self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2


                Result:



                img






                share|improve this answer


























                  3












                  3








                  3







                  In addition to @Stephan answer.



                  With less code:



                  self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2


                  Result:



                  img






                  share|improve this answer













                  In addition to @Stephan answer.



                  With less code:



                  self.countView.layer.cornerRadius = self.countView.bounds.size.height / 2


                  Result:



                  img







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 8:46









                  excitedmicrobeexcitedmicrobe

                  1,1131519




                  1,1131519






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465559%2fhow-to-make-an-uiview-into-circle-shape-in-swift-4-2%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







                      Popular posts from this blog

                      Tonle Sap (See)

                      I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                      Guatemaltekische Davis-Cup-Mannschaft