ios - Why did UIImageView did not load properly using Swift -
basically want create road hero move on before game starts, user can see first steps, problem when post code in override func viewdidload
load image appearance post in main.storyboard
not code,using print
shows images in place them need be, that's not see on screen. in alternative, place same code in @ibaction func play
, after press play button images load them need be.here code put in viewdidload:
@iboutlet weak var hero: uiimageview! @iboutlet weak var bigroad: uiimageview! @iboutlet weak var road: uiimageview! @iboutlet weak var roadtwo: uiimageview! @iboutlet weak var play: uibutton! override func viewdidload() { super.viewdidload() self.play.hidden = false self.hero.hidden = false self.bigroad.hidden = false self.road.hidden = false self.roadtwo.hidden = false self.hero.center.x = (width / 2) + 3 self.hero.center.y = ((height / 2.5) + (height / 3.5)) - 46 self.bigroad.center = cgpointmake(width / 2 , (height / 2.5) + (height / 3.5)) self.road.center = cgpointmake(width / 2 + 39, (height / 2.5) + (height / 3.5) - 29) self.roadtwo.center = cgpointmake((width / 2 ) + 78, ((height / 2.5) + (height / 3.5)) - 58)
as said, when put same code in @ibaction func play
, when func play starts in place. my question why images didn't load in place them need in viewdidload, loads in play function? do wrong , how can prevent mistake?
you need move frame code point, when autolayout processes finished. example:
var layedout = false override func viewdidlayoutsubviews() { super.viewdidlayoutsubviews() if !layedout { // perform frame code self.hero.center.x = (width / 2) + 3 self.hero.center.y = ((height / 2.5) + (height / 3.5)) - 46 self.bigroad.center = cgpointmake(width / 2 , (height / 2.5) + (height / 3.5)) self.road.center = cgpointmake(width / 2 + 39, (height / 2.5) + (height / 3.5) - 29) self.roadtwo.center = cgpointmake((width / 2 ) + 78, ((height / 2.5) + (height / 3.5)) - 58) } }
note viewdidlayoutsubviews
can called multiple times, need catch point when on screen , disable frame initialization code in method, flag example
override func viewdidappear(animated: bool) { super.viewdidappear(animated) layedout = true } override func viewwilldisappear(animated: bool) { super.viewwilldisappear(animated) layedout = false }
Comments
Post a Comment