calayer - Swift: Mask Alignment + Auto-Layout Constraints -


i have png file, i'd use mask uiview.

enter image description here

the view must be:

  • 20 pixels/points in each side
  • a perfect square
  • centered vertically

i set following constraints accomplish this:

enter image description here

however, seems these constraints don't play masks. when these constraints , mask property set, following:

enter image description here

but i'd view mask above, except orange (the backgroundcolor here simplicity—i later add subviews need masked.)

however, when no constraints set, mask seems work , (bordercolor added visual purposes only):

enter image description here

here's code (viewforlayer uiview made in storyboard):

    viewforlayer.layer.bordercolor = uicolor.redcolor().cgcolor     viewforlayer.layer.borderwidth = 10      var mask = calayer()     mask.contents = uiimage(named: "topbump")!.cgimage     mask.frame = cgrect(x: 0, y: 0, width: viewforlayer.bounds.width, height: viewforlayer.bounds.height)     mask.position = cgpoint(x: viewforlayer.bounds.width/2, y: viewforlayer.bounds.height/2)     viewforlayer.layer.mask = mask     viewforlayer.backgroundcolor = uicolor.orangecolor() 

the problem though, view isn't right size or in right position—it doesn't follow rules above—"the view must be: ". how can have mask work properly, , auto-layout constraints set @ same time?

i found way around it. not sure if best way here go...

http://imgur.com/puizbna

just make sure change name of uiview class in storyboard inspector too. apparently, trick set mask frame each layoutsubviews call.

class maskview : uiview {    override func layoutsubviews() {     super.layoutsubviews()     if let mask = self.layer.mask {       mask.frame = self.bounds     }           }  }  class viewcontroller: uiviewcontroller {      @iboutlet weak var viewforlayer: maskview!      override func viewdidload() {         super.viewdidload()         let image = uiimage(named: "topbump")!.cgimage!         let masklayer = calayer()         masklayer.contents = image         masklayer.frame = viewforlayer.bounds         viewforlayer.layer.mask = masklayer         viewforlayer.backgroundcolor = uicolor.orangecolor()          // additional setup after loading view, typically nib.         viewforlayer.layer.bordercolor = uicolor.redcolor().cgcolor         viewforlayer.layer.borderwidth = 10       }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }  } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -