ios - UIView center property is giving unexpected result -
i trying design screen programmatically. first, setup navigation bar. after add table view , after add plain uiview
. having trouble while using .center
property of last added uiview
. want add uiactivityindicatorview
view. code follows (using default height , width of large white activity indicator, 37pt each):
func setupactivityview() { let screen = uiscreen.mainscreen().bounds let view = uiview(frame: cgrectmake(0.0, self.navbar.frame.height, screen.width, uiscreen.mainscreen().bounds.height-self.navbar.frame.height)) view.backgroundcolor = uicolor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.80) //view.hidden = true self.activityview = view self.view.addsubview(view) let spinner = uiactivityindicatorview(activityindicatorstyle: .whitelarge) spinner.startanimating() spinner.hideswhenstopped = true self.spinner = spinner /*point:1*/ //spinner.center = view.center /*point:2*/ //spinner.frame.size = cgsizemake(37.0, 37.0) /*point:3*/ spinner.frame = cgrectmake((view.frame.width/2) - 18.5, (view.frame.height/2) - 18.5, 37.0, 37.0) view.addsubview(spinner) }
in above code, if see carefully, have commented point:1
point:2
, using point:3
. gives me following result (which correct , expected):
now, when use point:1
point:2
, comment point-3
following:
func setupactivityview() { let screen = uiscreen.mainscreen().bounds let view = uiview(frame: cgrectmake(0.0, self.navbar.frame.height, screen.width, uiscreen.mainscreen().bounds.height-self.navbar.frame.height)) view.backgroundcolor = uicolor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.80) //view.hidden = true self.activityview = view self.view.addsubview(view) let spinner = uiactivityindicatorview(activityindicatorstyle: .whitelarge) spinner.startanimating() spinner.hideswhenstopped = true self.spinner = spinner /*point:1*/ spinner.center = view.center /*point:2*/ spinner.frame.size = cgsizemake(37.0, 37.0) /*point:3*/ //spinner.frame = cgrectmake((view.frame.width/2) - 18.5, (view.frame.height/2) - 18.5, 37.0, 37.0) view.addsubview(spinner) }
i following result (which , guess, wrong , unexpected):
so difference 2 approaches , going wrong here? understanding of 2 approaches wrong? if yes happens here?
viewdidload
in method view width , height not properly. can print view width , height know reason.
viewdidappear
viewdidappear called when view visible, code have place here //now check it
Comments
Post a Comment