0

I have UIWebview and inside its scrollView I added multiple subviews, for arguments sake, lets say I placed on in the middle of the screen in portrait, when I rotate my screen to landscape, I want my subview to remain in that fixed position, so if one is in the middle of the screen, it should remain in the middle of the screen when rotated.

I have this:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    for views in webview.scrollView.subviews {
        views.frame = CGRectMake(?, ?, views.frame.width, views.frame.height)
    }
}

So far the subviews, width and height remain the same. I just need to figure out how to calculate so the UIViews will remain the same position inside the scrollView. Does anyone have any ideas at all?

UPDATE

I have tried the following after adding a subview:

let widthConstraint = NSLayoutConstraint(item: stampView, attribute: .Width, relatedBy: .Equal,
                                         toItem: nil, attribute: .NotAnAttribute,
                                         multiplier: 1.0, constant: 250)

let heightConstraint = NSLayoutConstraint(item: stampView, attribute: .Height, relatedBy: .Equal,
                                          toItem: nil, attribute: .NotAnAttribute,
                                          multiplier: 1.0, constant: 100)

let xConstraint = NSLayoutConstraint(item: stampView, attribute: .CenterX, relatedBy: .Equal,
                                     toItem: self.webview.scrollView, attribute: .CenterX,
                                     multiplier: 1, constant: 0)

let yConstraint = NSLayoutConstraint(item: stampView, attribute: .CenterY, relatedBy: .Equal,
                                     toItem: self.webview.scrollView, attribute: .CenterY,
                                     multiplier: 1, constant: 0)

NSLayoutConstraint.activateConstraints([widthConstraint, heightConstraint, xConstraint, yConstraint])

everything remains the same, the subviews is not in a fixed position.

I have also tried the following:

let widthConstraint = NSLayoutConstraint(item: stampView, attribute: .Width, relatedBy: .Equal,
                                         toItem: nil, attribute: .NotAnAttribute,
                                         multiplier: 1.0, constant: 250)
stampView.addConstraint(widthConstraint)

let heightConstraint = NSLayoutConstraint(item: stampView, attribute: .Height, relatedBy: .Equal,
                                          toItem: nil, attribute: .NotAnAttribute,
                                          multiplier: 1.0, constant: 100)
stampView.addConstraint(heightConstraint)

let xConstraint = NSLayoutConstraint(item: stampView, attribute: .CenterX, relatedBy: .Equal,
                                     toItem: self.webview.scrollView, attribute: .CenterX,
                                     multiplier: 1, constant: 0)
self.webview.scrollView.addConstraint(xConstraint)

let yConstraint = NSLayoutConstraint(item: stampView, attribute: .CenterY, relatedBy: .Equal,
                                     toItem: self.webview.scrollView, attribute: .CenterY,
                                     multiplier: 1, constant: 0)
self.webview.scrollView.addConstraint(yConstraint)

still did not work.

maxwell
  • 3,788
  • 6
  • 26
  • 40
user979331
  • 11,039
  • 73
  • 223
  • 418
  • can you just use constraints to accomplish this? like center horizontally and vertically in container constraints – Will M. Jul 26 '16 at 15:29
  • can u do constraints programmatically because I am adding the subviews programmatically – user979331 Jul 26 '16 at 15:31
  • 1
    yes you can http://stackoverflow.com/questions/27624133/programmatically-add-centerx-centery-constraints https://developer.apple.com/library/prerelease/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html – Will M. Jul 26 '16 at 15:32
  • I have updated my answer, please have a look Will M. – user979331 Jul 26 '16 at 15:42
  • is your scrollview well constrained? if your scrollviews center is moving around because its constrains are weak, then your stampView will match that center, even if its not the center of the screen – Will M. Jul 26 '16 at 15:44
  • There are no constrains to my scrollview – user979331 Jul 26 '16 at 15:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118338/discussion-between-will-m-and-user979331). – Will M. Jul 26 '16 at 15:58

1 Answers1

0

The scrollView inside a webView is internal to the webView. Adding subviews to it is unsupported behaviour. The only reason it is exposed as a public property is because:

Your app can access the scroll view if it wants to customize the scrolling behavior of the web view.

If you want the subview to have a fixed position w.r.t. the webView, then add both to the same superview, and setup constraints between the two.

fishinear
  • 6,101
  • 3
  • 36
  • 84