iphone - Custom View size in iOS not dynamic -
i have started first app on ios week ago. have created custom view use in app using appledeveloperwebsite custom rating control tutorial.
now have chosen iphone7 device in storyboard , run on iphone 7 emulator works when run on iphone 5 emulator (size of screen changes) custom views extend beyond screen. other controls sizes resize set constraints custom view sizes messed up.
please help
import uikit @ibdesignable class xradiobuttonview: uiview { var button: uibutton! var label: uilabel! @ibinspectable var text: string? { didset{ label.text = text } } //properties var isselected = 0 //initialization override init(frame: cgrect){ super.init(frame: frame) addsubviews() } required init(coder adecoder: nscoder) { super.init(coder: adecoder)! addsubviews() } func addsubviews() { self.backgroundcolor = uicolor.white let xwidth = bounds.size.width let xheight = bounds.size.height let tap = uitapgesturerecognizer(target: self, action: #selector(xradiobuttonview.radiobuttontexttapped)) button = uibutton(frame: cgrect(x: 1, y: 1, width: xwidth - 2, height: xheight - 4)) button.backgroundcolor = uicolor.white button.addtarget(self, action: #selector(xradiobuttonview.radiobuttontapped(button:)), for: .touchdown) addsubview(button) label = uilabel(frame: cgrect(x: 1, y: 1, width: xwidth - 2, height: xheight - 2)) label.textcolor = uicolor.init(hex: "#d5d5d5") //label.font = uifont.init(name: label.font.fontname, size: 25) //label.font = label.font.withsize(25) label.font = uifont.boldsystemfont(ofsize: 25) label.textalignment = nstextalignment.center label.isuserinteractionenabled = true label.addgesturerecognizer(tap) addsubview(label) } override func layoutsubviews() { // set button's width , height square size of frame's height. } override func prepareforinterfacebuilder() { super.prepareforinterfacebuilder() label.text = "xrbv" } func radiobuttontapped(button: uibutton) { if isselected == 0 { isselected = 1 self.backgroundcolor = uicolor.init(hex: "#00bfa5") label.textcolor = uicolor.init(hex: "#00bfa5") } else { isselected = 0 self.backgroundcolor = uicolor.white label.textcolor = uicolor.init(hex: "#d5d5d5") } } func radiobuttontexttapped(sender: uitapgesturerecognizer){ if isselected == 0 { isselected = 1 self.backgroundcolor = uicolor.init(hex: "#00bfa5") label.textcolor = uicolor.init(hex: "#00bfa5") } else { isselected = 0 self.backgroundcolor = uicolor.white label.textcolor = uicolor.init(hex: "#d5d5d5") } } }
can see pg button should finish green color finishes white color button extended beyond screen
you either need set frame in layoutsubviews or need implement autolayout in code:
button = uibutton(frame: cgrect(x: 1, y: 1, width: xwidth - 2, height: xheight - 4)) button.backgroundcolor = uicolor.white button.addtarget(self, action: #selector(xradiobuttonview.radiobuttontapped(button:)), for: .touchdown) addsubview(button) button.translatesautoresizingmaskintoconstraints = false let attributes: [nslayoutattribute] = [.top, .bottom, .leading, .trailing] let constants = [2, 2, 10, 10] // 2 top , bottom, 10 leading , trailing nslayoutconstraint.activate(attributes.enumerated().map { nslayoutconstraint(item: button, attribute: $1, relatedby: .equal, toitem: button.superview, attribute: $1, multiplier: 1, constant: constants[$0]) }) the example uses old way because constraints uniform, if have more complicated simpler use nslayoutanchor of ios 9.
edit: here code tuples if interested:
button.translatesautoresizingmaskintoconstraints = false let attributes: [(nslayoutattribute, cgfloat)] = [(.top, 2), (.bottom, 2), (.leading, 12), (.trailing, 12)] nslayoutconstraint.activate(attributes.map { nslayoutconstraint(item: button, attribute: $0.0, relatedby: .equal, toitem: button.superview, attribute: $0.0, multiplier: 1, constant: $0.1) })
Comments
Post a Comment