ios - Swift - Keep UIImageView in the same position on rotate -


i have uiwebview , in scrollview, added uiimageview so:

self.webview.scrollview.addsubview(image) 

my problem when rotate device portrait landscape uiimageview not stay @ position set on scrollview, understand width , height of screen change, guess trying change the position on uiimageview appears did not change.

i have method in place:

 override func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) {          var newscrollviewframe = webview.scrollview.frame         var newframe = webview.frame          newframe.size.width = size.width         newframe.size.height = size.height          newscrollviewframe.size.width = size.width         newscrollviewframe.size.height = size.height          webview.frame = newframe         webview.scrollview.frame = newscrollviewframe      } 

the current code inside method resize uiwebview , scroll view, not uiimageviews in scrollview

any appreciated.

thanks,

i have tried following:

for views in webview.scrollview.subviews         {             if(views.iskindofclass(uiimageview))             {                 views.transform = cgaffinetransformmakerotation(cgfloat(m_pi)/2);              }         } 

but puts on uiimageview sideways on rotate

here how adding webview view:

webview = uiwebview()          webview.frame = self.view.bounds         webview.scrollview.frame = webview.frame          webview.userinteractionenabled = true         webview.scalespagetofit = true         webview.becomefirstresponder()         webview.delegate = self         webview.scrollview.delegate = self         self.view.addsubview(webview) 

i have half solved problem, doing following:

webview.translatesautoresizingmaskintoconstraints = false  let horizontalconstraint = nslayoutconstraint(item: webview, attribute: nslayoutattribute.centerx, relatedby: nslayoutrelation.equal, toitem: view, attribute: nslayoutattribute.centerx, multiplier: 1, constant: 0)         view.addconstraint(horizontalconstraint)          let verticalconstraint = nslayoutconstraint(item: webview, attribute: nslayoutattribute.centery, relatedby: nslayoutrelation.equal, toitem: view, attribute: nslayoutattribute.centery, multiplier: 1, constant: 0)         view.addconstraint(verticalconstraint)          let widthconstraint = nslayoutconstraint(item: webview, attribute: nslayoutattribute.width, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 0.1, constant: 500)         view.addconstraint(widthconstraint)          let heightconstraint = nslayoutconstraint(item: webview, attribute: nslayoutattribute.height, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1, constant: 500)         view.addconstraint(heightconstraint) 

however now, uiwebview not full width or height :( sooooo close.

i tried this, uiimageview not remain in same spot.

let left = nslayoutconstraint(item: self.webview, attribute: .left, relatedby: .equal, toitem: self.view, attribute: .left, multiplier: 1, constant: 0)   let right = nslayoutconstraint(item: self.webview, attribute: .right, relatedby: .equal, toitem: self.view, attribute: .right, multiplier: 1, constant: 0)   let top = nslayoutconstraint(item: self.webview, attribute: .top, relatedby: .equal, toitem: self.view, attribute: .top, multiplier: 1, constant: 0)   let bottom = nslayoutconstraint(item: self.webview, attribute: .bottom, relatedby: .equal, toitem: self.view, attribute: .bottom, multiplier: 1, constant: 0)   nslayoutconstraint.activateconstraints([top, bottom, left, right]) 

i have tried adding constraints uiimageview

let stampview:stampannotation = stampannotation(imageicon: uiimage(named: "approved.png"), location: cgpointmake(currentpoint.x, currentpoint.y))             self.webview.scrollview.addsubview(stampview)              let left = nslayoutconstraint(item: stampview, attribute: .left, relatedby: .equal, toitem: self.webview.scrollview, attribute: .left, multiplier: 1, constant: 0)              let right = nslayoutconstraint(item: stampview, attribute: .right, relatedby: .equal, toitem: self.webview.scrollview, attribute: .right, multiplier: 1, constant: 0)              let top = nslayoutconstraint(item: stampview, attribute: .top, relatedby: .equal, toitem: self.webview.scrollview, attribute: .top, multiplier: 1, constant: 0)              let bottom = nslayoutconstraint(item: stampview, attribute: .bottom, relatedby: .equal, toitem: self.webview.scrollview, attribute: .bottom, multiplier: 1, constant: 0)              nslayoutconstraint.activateconstraints([top, bottom, left, right]) 

same result, uiimageview not stay in same spot.

update

my webview:

webview = uiwebview()         webview.userinteractionenabled = true         webview.scalespagetofit = true         webview.becomefirstresponder()         webview.delegate = self         webview.scrollview.delegate = self         self.view.addsubview(webview)          webview.loadrequest(nsurlrequest(url:url))         webview.gesturerecognizers = [pinchrecognizer, panrecognizer]         webview.translatesautoresizingmaskintoconstraints = false          let left = nslayoutconstraint(item: webview, attribute: .left, relatedby: .equal, toitem: self.view, attribute: .left, multiplier: 1, constant: 0)          let right = nslayoutconstraint(item: webview, attribute: .right, relatedby: .equal, toitem: self.view, attribute: .right, multiplier: 1, constant: 0)          let top = nslayoutconstraint(item: webview, attribute: .top, relatedby: .equal, toitem: self.view, attribute: .top, multiplier: 1, constant: 0)          let bottom = nslayoutconstraint(item: webview, attribute: .bottom, relatedby: .equal, toitem: self.view, attribute: .bottom, multiplier: 1, constant: 0)          nslayoutconstraint.activateconstraints([top, bottom, left, right]) 

uiimageview

stampview.translatesautoresizingmaskintoconstraints = false              let left = nslayoutconstraint(item: stampview, attribute: .left, relatedby: .equal, toitem: self.webview.scrollview, attribute: .left, multiplier: 1, constant: 100)              let right = nslayoutconstraint(item: stampview, attribute: .right, relatedby: .equal, toitem: self.webview.scrollview, attribute: .right, multiplier: 1, constant: 0)              let top = nslayoutconstraint(item: stampview, attribute: .top, relatedby: .equal, toitem: self.webview.scrollview, attribute: .top, multiplier: 1, constant: 100)              let bottom = nslayoutconstraint(item: stampview, attribute: .bottom, relatedby: .equal, toitem: self.webview.scrollview, attribute: .bottom, multiplier: 1, constant: 0)              let width = nslayoutconstraint(item: stampview, attribute: nslayoutattribute.width, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 0.1, constant: 150)              let height = nslayoutconstraint(item: stampview, attribute: nslayoutattribute.height, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1, constant: 73)              nslayoutconstraint.activateconstraints([left, right, top, bottom, width, height]) 

i need figure out math have device @ position of touch. (in percentages ?)

i think these things easier without using autolayout. this, recomend using viewdidlayoutsubviews(). here code:

override func viewdidlayoutsubviews() {     super.viewdidlayoutsubviews()      webview.frame = view.bounds      let screen = uiscreen.mainscreen().fixedcoordinatespace      //these values give rect half size of screen , centered.     let width = screen.bounds.width / 2     let height = screen.bounds.height / 2     let x = (screen.bounds.width - width) / 2     let y = (screen.bounds.height - height) / 2     let absoluterect = cgrect(x: x, y: y, width: width, height: height)      let stamprect = screen.convertrect(absoluterect, tocoordinatespace: webview)     stampview.frame = stamprect      //change orientation of image     switch uidevice.currentdevice().orientation {     case .landscapeleft:         stampview.image = uiimage(cgimage:originalimage.cgimage!, scale:originalimage.scale, orientation: uiimageorientation.left)         break     case .landscaperight:         stampview.image = uiimage(cgimage:originalimage.cgimage!, scale:originalimage.scale, orientation: uiimageorientation.right)         break     default:         stampview.image = uiimage(cgimage:originalimage.cgimage!, scale:originalimage.scale, orientation: uiimageorientation.up)         break     } } 

i doing several things here...first set webviewframe. next, helpful here define absolute coordinate system relative screen. when phone orientation changes, values of screen not change (allowing keep imageview in same place.) next define desired frame stampview, convert absolute frame equivalent inside scrollview (it's superview) , assign stampview. finally, if want image in stampview oriented correctly, need change image orientation. cgaffinetransformmakerotation works if view square, can make more general changing imageorientation within stampview. requires property original image:

let originalimage = uiimage(named: "image_name")! 

finally, because viewwilllayoutsubviews() not called landscape landscape transitions, following:

override func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) {     view.setneedslayout() } 

this code doesn't make prettiest transitions, should layout problems.


Comments