ios - View size is automatically being set to window size, when one of window's subview is removed -
i have 2 views 1 of them mainvideoview (grey color) , other subvideoview (red color). both of them subviews of uiapplication.shared.keywindow
.
when try , minimise them (using func minimiseormaximiseviews
, minimised (as shown in below image).
after which, remove subvideoview window. moment try , remove subvideoview (using func removesubvideoviewfromvideoview()
called func minimiseormaximiseviews
), mainvideoview enlarges full screen size, not sure why happening, want stay @ same size.
could please advise/ suggest how achieve ?
this how setting views
func configurevideoview(){ videoview.backgroundcolor = uicolor.darkgray videoview.tag = 0 // identify view during animation // adding tap gesture maximise view when small let tapgesturerecognizer = uitapgesturerecognizer(target: self, action: #selector(handletap(gesturerecognizer:))) videoview.addgesturerecognizer(tapgesturerecognizer) // adding pan gesture recogniser make videoview movable inside parent view videoview.addgesturerecognizer(uipangesturerecognizer(target: self, action: #selector(self.dragview))) guard let window = uiapplication.shared.keywindow else{ return } window.addsubview(videoview) videoview.translatesautoresizingmaskintoconstraints = false let viewsdict = ["videoview" : videoview] [string : any] // setting constraints window.addconstraints(nslayoutconstraint.constraints(withvisualformat: "h:|[videoview]|", options: [], metrics: nil, views: viewsdict)) window.addconstraints(nslayoutconstraint.constraints(withvisualformat: "v:|[videoview]|", options: [], metrics: nil, views: viewsdict)) window.layoutifneeded() // lays out subviews window.bringsubview(tofront: videoview) // bring subview front print("videoview constraints in configurevideoview \(videoview.constraints)") } func configuresubvideoview(){ subvideoview.backgroundcolor = uicolor.red subvideoview.tag = 1 // identify view during animation subvideoview.ishidden = true // adding pan gesture recogniser make subvideoview movable inside parentview subvideoview.addgesturerecognizer(uipangesturerecognizer(target: self, action: #selector(self.dragview))) // constraining subvideoview window ensure minimising , maximising animation works constrainsubvideoviewtowindow() } func constrainsubvideoviewtowindow(){ //self.subvideoview.setneedslayout() guard let window = uiapplication.shared.keywindow else{ print("window not exist") return } guard !window.subviews.contains(subvideoview)else{ // not allow go through below code if window contains subvideoview return } if self.videoview.subviews.contains(subvideoview){ // if videoview contains subvideoview remove subvideoview subvideoview.removefromsuperview() } window.addsubview(subvideoview) // default constraints ensure subvideoview initialised maxsubvideoviewwidth & maxsubvideoviewheight , positioned above buttons let bottomoffset = buttondiameter + buttonstackviewbottompadding + padding subvideoview.translatesautoresizingmaskintoconstraints = false let widthconstraint = nslayoutconstraint(item: subvideoview, attribute: .width, relatedby: .equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1.0, constant: maxsubvideoviewwidth) let heightconstraint = nslayoutconstraint(item: subvideoview, attribute: .height, relatedby: .equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1.0, constant: maxsubvideoviewheight) let rightconstraint = nslayoutconstraint(item: subvideoview, attribute: .trailing, relatedby: .equal, toitem: window, attribute: .trailing, multiplier: 1.0, constant: -padding) let bottomconstraint = nslayoutconstraint(item: subvideoview, attribute: .bottom, relatedby: .equal, toitem: window, attribute: .bottom, multiplier: 1.0, constant: -bottomoffset) var constraintsarray = [nslayoutconstraint]() constraintsarray.append(widthconstraint) constraintsarray.append(heightconstraint) constraintsarray.append(rightconstraint) constraintsarray.append(bottomconstraint) window.addconstraints(constraintsarray) window.layoutifneeded() // lays out subviews subvideoview.setviewcornerradius() window.bringsubview(tofront: subvideoview) // bring subview front }
this how animating views , removing subvideoview
func minimiseormaximiseviews(animationtype: string){ let window = uiapplication.shared.keywindow let buttonstackviewheight = buttonsstackview.frame.height if animationtype == "maximiseview" { constrainsubvideoviewtowindow() } uiview.animate(withduration: 0.3, delay: 0, options: [], animations: { [unowned self] in // "[unowned self] in" added avoid strong reference cycles, switch animationtype { case "minimiseview" : self.hidecontrols() // minimising self i.e videoview self.videoview.frame = cgrect(x: self.mainscreenwidth - self.videoviewwidth - self.padding, y: self.mainscreenheight - self.videoviewheight - self.padding, width: self.videoviewwidth, height: self.videoviewheight) // minimising subvideoview self.subvideoview.frame = cgrect(x: self.mainscreenwidth - self.minsubvideoviewwidth - self.padding * 2, y: self.mainscreenheight - self.minsubvideoviewheight - self.padding * 2, width: self.minsubvideoviewwidth, height: self.minsubvideoviewheight) window?.layoutifneeded() self.videoview.setviewcornerradius() self.subvideoview.setviewcornerradius() print("self.subvideoview.frame after setting: \(self.subvideoview.frame)") default: break } }) { [unowned self] (finished: bool) in // called when animation completes, , finished value true if animationtype == "minimiseview" { // **** removing subvideoview videoview **** self.removesubvideoviewfromvideoview() } } } func removesubvideoviewfromvideoview(){ //self.subvideoview.setneedslayout() guard let window = uiapplication.shared.keywindow else{ return } guard !self.videoview.subviews.contains(subvideoview)else{ // not allow go through below code if videoview contains subvideoview return } if window.subviews.contains(subvideoview) { // removing subvideoview window subvideoview.removefromsuperview() // *** cause of issue *** } guard !window.subviews.contains(subvideoview) else{ return } videoview.addsubview(subvideoview) }
the videoview , subvideoview layout auto layout. in auto layout if want change frame, have update constraints.
calling removefromsuperview
removes constraints refer view removing, or refer view in subtree of view removing. means auto layout system rearrange views.
Comments
Post a Comment