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.