I have a UIButton which I've added in IB, however the view behind this button changes color and I need to switch the button's type between InfoDark and InfoLight. Bizarrely though, buttonType property is read only. So how can i switch between light and dark info buttons?
            Asked
            
        
        
            Active
            
        
            Viewed 4.6k times
        
    30
            
            
         
    
    
        GingerHead
        
- 8,130
- 15
- 59
- 93
 
    
    
        Jonathan.
        
- 53,997
- 54
- 186
- 290
- 
                    2This is not a duplicate. This question is about changing the button type after it is instantiated. The referenced question is about how to instantiate the button in code. Please remove the duplication. – osxdirk Aug 24 '17 at 09:25
- 
                    swift 5.2 : self.btnFont.buttonType = UIButton.ButtonType.system – Ucdemir Nov 13 '20 at 14:49
2 Answers
54
            
            
        You can't set buttonType readonly property, use below may help:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 
    
    
        anhduongt
        
- 1,119
- 2
- 12
- 17
- 
                    15
- 
                    In swift , If you are using custome class then, let button = MyCustomeClass.init(type: .custom) – MiTal Khandhar Feb 02 '18 at 04:52
33
            There's no way to change the .buttonType once it's set.
You can prepare two buttons, and hide one of them depending on the background color.
 
    
    
        kennytm
        
- 510,854
- 105
- 1,084
- 1,005
- 
                    well apparently theres a private setter method, by why something like that is private who knows. Thanks for you idea about 2 buttons, that's what I'll do. – Jonathan. Jun 20 '10 at 17:38
- 
                    @Jon: It's private because setting this will *not* convert a dark info button to a light one. – kennytm Jun 20 '10 at 19:08