ios - addSubView - adding a button inside a UIView adds the same button for it's SuperView as well -
i following tutorial create rating button - https://developer.apple.com/library/prerelease/ios/referencelibrary/gettingstarted/developiosappsswift/lesson5.html
i created uiview inside uiviewcontroller inside storyboard drag , drop , uiview controlled below class.
class ratingcontrol: uiview { required init(coder adecoder: nscoder) { super.init(coder: adecoder) let button = uibutton(frame: cgrect(x: 0, y: 0, width: 44, height: 44)) button.backgroundcolor = uicolor.redcolor() button.addtarget(self, action: "ratingbuttontapped:", forcontrolevents: .touchdown) addsubview(button) } override func intrinsiccontentsize() -> cgsize { return cgsize(width: 320, height: 44) } func ratingbuttontapped(button: uibutton) { print("button pressed 👍") } }
the problem when add uibutton, it's showing 2 buttons, 1 in it's parent uiviewcontroller @ location (x:0, y:0) in addition 1 inside uiview. want button showing inside uiview inside uiviewcontroller.
i researched ways prevent it, 1 way add these buttons manually in storyboard instead of creating them in init, curious how works fine in tutorial. using ios 8.3.
screenshot:
you must have subclassed uiviewcontrollers view class ratingcontrol , view have added because of showing twice. kindly check because works proper if u add view need
if have added subclass viewcontrollers view , per scenario
Comments
Post a Comment