I'm a newbie, new to Swift. The uiView is superimposed on top of the webview.
self.view.addSubview(webView)
self.view.addSubview(chattingWebView)
webView.didMoveToSuperview()
chattingWebView.didMoveToSuperview()
// rotate
webView.translatesAutoresizingMaskIntoConstraints = false
webViewTopAnchor = webView.topAnchor.constraint(equalTo: self.view.layoutMarginsGuide.topAnchor)
webViewTopAnchor.isActive = true
webViewCenterYAnchor = webView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
webViewCenterYAnchor.isActive = false
webViewWidthAnchor = webView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width)
webViewWidthAnchor.isActive = true
webViewHeightAnchor = webView.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.5625)
webViewHeightAnchor.isActive = true
uiViewFragContainer = LiveSettingChangeChattingBGViewHolder(height: UIScreen.main.bounds.width * 0.5625)
//uiViewFragContainer.updateView(vodItemBean: self.vodItemBean)
self.view.addSubview(uiViewFragContainer)
uiViewFragContainer.translatesAutoresizingMaskIntoConstraints = false
uiViewFragContainer.topAnchor.constraint(equalTo: self.webView.topAnchor).isActive = true
uiViewFragContainerLeadingAnchor = uiViewFragContainer.leadingAnchor.constraint(equalTo: self.webView.leadingAnchor)
uiViewFragContainerLeadingAnchor.isActive = true
uiViewFragContainerTrailingAnchor = uiViewFragContainer.trailingAnchor.constraint(equalTo: self.webView.trailingAnchor)
uiViewFragContainerTrailingAnchor.isActive = true
uiViewFragContainer.bottomAnchor.constraint(equalTo: self.webView.bottomAnchor).isActive = true
uiViewFragContainer.isHidden = true
The default is vertical lock and the button rotates the screen. Since the height of the webview changes when the screen is rotated, I used the following method.
Rotate the screen by grasping the current state when clicking the button
if(self.orientation == .portrait) { self.setOrientation(.landscapeRight) } else if(self.orientation == .landscapeRight) { self.setOrientation(.portrait) }Call a function that changes the constraint while rotating the screen
private func setOrientation(_ orientation : UIInterfaceOrientation) { if orientation == .portrait { self.appDelegate.orientation = [.portrait] self.orientation = .portrait fullscreen = false UIDevice.current.setValue(orientation.rawValue, forKey: "orientation") self.changeWebViewFrame(fullscreen: fullscreen) } else if orientation == .landscapeRight { self.appDelegate.orientation = [.portrait, .landscapeRight] self.orientation = .landscapeRight fullscreen = true UIDevice.current.setValue(orientation.rawValue, forKey: "orientation") self.changeWebViewFrame(fullscreen: fullscreen) }Rotate the screen
private func changeWebViewFrame(fullscreen : Bool) { if(fullscreen) { webViewWidthAnchor.constant = UIScreen.main.bounds.width webViewWidthAnchor.isActive = true webViewHeightAnchor.constant = UIScreen.main.bounds.height webViewHeightAnchor.isActive = true chattingTopAnchor.isActive = false chattingHeightAnchor.constant = UIScreen.main.bounds.height * 0.5 chattingHeightAnchor.isActive = true chattingWidthAnchor.constant = UIScreen.main.bounds.width chattingWidthAnchor.isActive = true self.opacity = Float(0.1) if(self.isBlack) { self.chattingWebView.backgroundColor = UIColor.black.withAlphaComponent(CGFloat(opacity)) } else { self.chattingWebView.backgroundColor = UIColor.white.withAlphaComponent(CGFloat(opacity)) } self.view.layoutIfNeeded() self.view.updateConstraints() DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) { print("???????") //self.test() } //test() } else { webViewWidthAnchor.constant = UIScreen.main.bounds.width webViewWidthAnchor.isActive = true webViewHeightAnchor.constant = UIScreen.main.bounds.width * 0.5625 webViewHeightAnchor.isActive = true chattingTopAnchor.isActive = true chattingHeightAnchor.isActive = false chattingWidthAnchor.constant = UIScreen.main.bounds.width chattingWidthAnchor.isActive = true self.opacity = Float(1.0) if(self.isBlack) { self.chattingWebView.backgroundColor = UIColor.black.withAlphaComponent(CGFloat(opacity)) } else { self.chattingWebView.backgroundColor = UIColor.white.withAlphaComponent(CGFloat(opacity)) } }}
Unfortunately leadingAnchor and trailingAnchor do not work properly. I tried various ways to apply this.
After .isActive = false, set the Anchor value again and set isActive = true
Using NsConstraint.active([])
Using view.setNeedsUpdateConstraints
All three of these methods did not work. What could be the reason?