swift - IOS Cant Reposition UIImageView at runtime -
i have looked @ other questions on regarding issue , code seems follow provided solutions don't know going wrong. ma trying reset position of image view when screen loads. size depend on base image.
the image remains placed in interface builder. there no constraints on image view.
does matter call from? have tried viewdidload()
, viewwillload()
, viewdidappear()
code
@iboutlet weak var camerapreview: uiview! @iboutlet weak var glassesoverlay: uiimageview! print("before") print("overlay x: \(glassesoverlay.frame.origin.x)") print("overlay y: \(glassesoverlay.frame.origin.y)") print("overlay w: \(glassesoverlay.frame.width)") print("overlay h: \(glassesoverlay.frame.height)") let glassesorigin = cgpointmake( (camerapreview.frame.width / 2) - (glassesoverlay.frame.width / 2), camerapreview.frame.height / 3) //let glassesorigin = cgpointmake( 179.0, 179.0) let glasseswidth = camerapreview.frame.width / 2.0 let glassessize = cgsizemake(glasseswidth, glasseswidth / 4) // glassesoverlay.frame.origin = cgpointmake( (camerapreview.frame.width / 2) - (glassesoverlay.frame.width / 2), camerapreview.frame.height / 3) // glassesoverlay.frame.size = glassessize glassesoverlay.frame = cgrect(origin: glassesorigin, size: glassessize) //camerapreview.subviews[0].frame = cgrect(origin: glassesorigin, size: glassessize) glassesoverlay.layer.bordercolor = uicolor.purplecolor().cgcolor glassesoverlay.layer.borderwidth = 2.0 camerapreview.layer.bordercolor = uicolor.purplecolor().cgcolor camerapreview.layer.borderwidth = 2.0 print("after") print("overlay x: \(glassesoverlay.frame.origin.x)") print("overlay y: \(glassesoverlay.frame.origin.y)") print("overlay w: \(glassesoverlay.frame.width)") print("overlay h: \(glassesoverlay.frame.height)")
as can see have tried explicitly setting values same result. printing position , dimensions before , after , print changed values.
print output
before overlay x: 135.0 overlay y: 382.0 overlay w: 320.0 overlay h: 128.0 after overlay x: 224.0 overlay y: 296.667 overlay w: 384.0 overlay h: 96.0
but remains unmoved.
are using auto layout? modify constraints instead of manipulating frame directly. after modifying constraints, need to
view.setneedslayout() view.layoutifneeded()
if not using auto layout, should work, except old frame matching new frame or outlet holding view expect.
edit:
please have @ how autolayout works. there wwdc videos explaining well.
Comments
Post a Comment