ios - How to automatically update the child view when the parent view's frame changes? -
i revising custom keyboard application. first attempt using stack views layout views had performance hit of 4-6 seconds of delay. so, making new custom keyboard using custom view , calling layoutsubviews()
well, layout views.
now, have made custom view represents keyboard. way, hardcoding values keyboard size. so, 1 work iphone 5 , 5s only. ( i'm checking view's frame width this.)
now, want make keyboard's view controller update custom view's frame whenever view controller bounds update!
how set custom view parent fill parent width , height such updates custom view's bounds/frames whenever view controller's bound change?
here's code far.
import uikit class keyboardviewcontroller: uiinputviewcontroller { var keyboardview: keyboardview? override func updateviewconstraints() { super.updateviewconstraints() // add custom view sizing constraints here } override func viewdidload() { super.viewdidload() view.autoresizessubviews = true keyboardview = keyboardview(frame: view.frame) view.addsubview(keyboardview!) } }
and, here's code custom view.
import uikit class keyboardview: uiview { // mark:- properties var row1buttonlabels = ["q","w","e","r","t","y","u","i","o","p"] var row1buttons = [uibutton]() var row2buttons = [uibutton]() var row3buttons = [uibutton]() var row4buttons = [uibutton]() override init(frame: cgrect) { super.init(frame: frame) in 0..<10 { let button = uibutton(frame: cgrect(x: 0, y: 12, width: 26, height: 38)) button.settitle(row1buttonlabels[i], forstate: .normal) button.addtarget(self, action: selector("printcharacter"), forcontrolevents: .touchdown) button.backgroundcolor = uicolor.blackcolor().colorwithalphacomponent(0.45) button.layer.cornerradius = 4.0 row1buttons.append(button) addsubview(button) } } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) in 0..<10 { let button = uibutton(frame: cgrect(x: 0, y: 12, width: 26, height: 38)) button.settitle(row1buttonlabels[i], forstate: .normal) button.addtarget(self, action: selector("printcharacter"), forcontrolevents: .touchdown) button.backgroundcolor = uicolor.blackcolor().colorwithalphacomponent(0.45) button.layer.cornerradius = 4.0 row1buttons.append(button) addsubview(button) } } override func layoutsubviews() { print("layoutsubviews") if frame.width == 320 { var buttonframe = cgrect(x: 0, y: 12, width: 26, height: 38) (index, button) in row1buttons.enumerate() { // if button first button, add leadingspace // else ( leadingspace + (buttonwidth + interbuttonspace) * index ) let xposition = index == 0 ? 3 : (3 + 26 * index + 6 * index) buttonframe.origin.x = cgfloat(xposition) button.frame = buttonframe } } else if frame.width == 568 { var buttonframe = cgrect(x: 0, y: 6, width: 47, height: 32) (index, button) in row1buttons.enumerate() { // if button first button, add leadingspace // else ( leadingspace + (buttonwidth + interbuttonspace) * index ) let xposition = index == 0 ? 26 : (26 + 47 * index + 5 * index) buttonframe.origin.x = cgfloat(xposition) print("frame: \(buttonframe)") button.frame = buttonframe } } } // ignore now, testing if keyboard working or not. and, worked. func printcharacter(){ button in row1buttons { button.settitle( button.titlelabel?.text?.lowercasestring , forstate: .normal) } } }
note: implemented init(coder) testing in storyboard. so, please ignore well.
also, custom keyboard working fine , running on 2-3 seconds delay on iphone 6s. but, that's expected having background image also.
moving question, know layoutsubviews called whenever custom view's frame changes. want custom view take full frame size of keyboard view controller keyboard should take full frame size, expected.
requested:
how set custom view parent fill parent width , height such updates custom view's bounds/frames whenever view controller's bound change?
i not using auto layout this, though enabled fairly. so, done programatically.
i tracking onto , studying documentation uiview. have come in touch 2 properties: autoresizingmask
, autoresizessubviews
, , checking if me on this. choosing layout subviews myself because performance hit high in case of auto layouts. that's personal preference.
again, interested in helping me, hardcoding each device. hardcoding 320x568 i.e. iphone 5 , 5s. custom dimension in use keyboard 320x216pt , 568x162pt.
update: every time controller updates bounds, implicitly want set frame of custom view parent's bound. done viewwilllayoutsubviews()
and/or viewdidlayoutsubviews()
. want set following frame size. did size in method.
is appropriate update subviews viewwilllayoutsubviews()
, / or viewdidlayoutsubviews?
cgrect(x: 0,y: 0, width: parent_width, height: parent_height)
kind regards, ariel
Comments
Post a Comment