ios - Add subviews on Customize UIView works not as expect -
i subclass class of uiview
called:aurtabview
. drag 3 views(white background) storyboard auto layout them 1/3 screen width of each. add uibutton
on init
method, works far below:
then want add 2 uiview
s on them. weird thing first , third autrabview
works expect, middle 1 doesn't.
this weird. checked uiview hierarchy below:
any point?
here code:
class aurtabview: uiview { let tabbutton = uibutton() let smallcircle = uiview() let largecircle = uiview() required init?(coder adecoder: nscoder) { super.init(coder: adecoder) self.addsubview(tabbutton) self.addsubview(smallcircle) self.addsubview(largecircle) } override func layoutsubviews() { super.layoutsubviews() let height = self.frame.height tabbutton.frame = cgrect(x: (self.frame.width-height)/2, y: 0, width: height, height: height) tabbutton.backgroundcolor = uicolor.greencolor() smallcircle.frame = cgrect(x: cgrectgetmidx(self.frame)-2.5, y: height-10-8, width: 5, height: 5) smallcircle.backgroundcolor = uicolor.redcolor() largecircle.frame = cgrect(x: cgrectgetmidx(self.frame)-5, y: height-8, width: 10, height: 10) largecircle.backgroundcolor = uicolor.redcolor() print(smallcircle) print(largecircle) } override func drawrect(rect: cgrect) { tabbutton.layer.cornerradius = tabbutton.frame.width/2 } }
use this
import uikit
class aurtabview: uiview {
let tabbutton = uibutton() let smallcircle = uiview() let largecircle = uiview() required init?(coder adecoder: nscoder) { super.init(coder: adecoder) self.addsubview(tabbutton) self.addsubview(smallcircle) self.addsubview(largecircle) } override func layoutsubviews() { super.layoutsubviews() let height = self.frame.height tabbutton.frame = cgrect(x: (self.frame.width-height)/2, y: 0, width: height, height: height) tabbutton.backgroundcolor = uicolor.greencolor() smallcircle.frame = cgrect(x: self.frame.width/2 - 2.5, y: height-10-8, width: 5, height: 5) smallcircle.backgroundcolor = uicolor.blackcolor() largecircle.frame = cgrect(x: self.frame.width/2 - 5, y: height-8, width: 10, height: 10) largecircle.backgroundcolor = uicolor.redcolor() print(smallcircle) print(largecircle) } override func drawrect(rect: cgrect) { tabbutton.layer.cornerradius = tabbutton.frame.width/2 }
}
Comments
Post a Comment