Can I change the properties of all buttons in my Swift application?

I have a Swift application that navigates through many view controllers, all with similar appearance. Each view controller has buttons for which I did NOT create IBOutlet properties (they all just run segues through Interface Builder).

Is there an easy way to change the background color or text color of all buttons in an application without changing it for each button in IB or creating an IBOutlet for each button?

+4
source share
2 answers

Here are the basic steps that will simplify the situation, in your case it will need to be repeated for each viewController.

  • Create one outlet for all of your buttons:

    @IBOutlet var myButtons: [UIButton]!
    
  • () . " Outlet", connections inspector ( ​​). , myButtons.

  • for:

    for button in self.myButtons {
        button.backgroundColor = UIColor.clearColor()
        button.layer.borderWidth = 1
        button.layer.borderColor = UIColor.greenColor().CGColor
    }
    

, . , , , , : Objective-C, Swift.

. , buttonStyle viewController for: [buttonStyle styleButton:button]. styleButton ( ).

by @zisoft: UIViewController Swift, !.

+6

appDelegate ( ):

let buttonAppearance = UIButton.appearance()
    buttonAppearance.tintColor = UIColor(red: 1.0, green:1.0, blue:1.0, alpha:1.0)

, . navigationBar, tabBar ..

0

All Articles