ios - Custom UIButton subclass not displaying title -
let me start off saying aware asked question, can't seem find same scenario/problem me.
i writing music application, , came ui liked. required button special functionality (past can achieved custom button type) decided make uibutton
subclass. used following code in subclass:
required init(coder adecoder: nscoder) { self.activestate = false self.activeaccidental = uiimageview() super.init(coder: adecoder) self.layer.bordercolor = uicolor(white:221/255, alpha: 1.0).cgcolor self.layer.borderwidth = 2.0 self.backgroundcolor = uicolor.blackcolor() self.settitlecolor(uicolor.whitecolor(), forstate: .normal) self.settitle("hello", forstate: .normal) } override func layoutsubviews() { println(self.frame.origin.x) self.activeaccidental = uiimageview(frame: cgrectmake(self.bounds.origin.x, self.bounds.origin.y, 20, 20)) self.activeaccidental.image = uiimage(named: "bmicalcicon.png") self.addsubview(activeaccidental) }
however, when add button storyboard (and enter custom class name in field) title, regardless if set in initializer shown, or in attributes inspector in storyboard, not visible. first major project in swift, not sure entirely problem is.
code when imageview
moved initwithcoder
required init(coder adecoder: nscoder) { self.activestate = false self.activeaccidental = uiimageview() super.init(coder: adecoder) self.activeaccidental = uiimageview(frame: cgrectmake(self.bounds.origin.x, self.bounds.origin.y, 20, 20)) self.activeaccidental.image = uiimage(named: "bmicalcicon.png") self.layer.bordercolor = uicolor(white:221/255, alpha: 1.0).cgcolor self.layer.borderwidth = 2.0 self.backgroundcolor = uicolor.blackcolor() self.settitlecolor(uicolor.whitecolor(), forstate: .normal) self.settitle("hello", forstate: .normal) self.addsubview(activeaccidental) }
i'm not sure why, problem putting code adding image view in layoutsubviews -- that's not place anyway, since can called multiple times, cause add multiple image views. if move code initwithcoder method, work properly. here's test class made,
import uikit class rdbutton: uibutton { var activestate: bool var activeaccidental: uiimageview required init(coder adecoder: nscoder) { self.activestate = false self.activeaccidental = uiimageview() super.init(coder: adecoder) self.activeaccidental = uiimageview(frame: cgrectmake(self.bounds.origin.x, self.bounds.origin.y, 20, 20)) self.activeaccidental.image = uiimage(named: "img.jpg") self.layer.bordercolor = uicolor(white:221/255, alpha: 1.0).cgcolor self.layer.borderwidth = 2.0 self.backgroundcolor = uicolor.blackcolor() self.settitlecolor(uicolor.whitecolor(), forstate: .normal) self.settitle("hello", forstate: .normal) self.addsubview(activeaccidental) } }
Comments
Post a Comment