- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change a button background color using Swift?
To change the background color of a button in iOS application we need to access the property ‘ backgroundColor’ of the UIButton. We can do this in two ways, programmatically and using the storyboard.
Method 1 − Using the storyboard editor
Add a button on your storyboard, select it Go to it’s attribute inspector and select 'Background' property to choose the color.
Method 2 − Programmatically changing the background
Create outlet of the button on the View Controller.
In the viewDidLoad() or viewWillLayoutSubview() method add the code to change the background color.
btn.backgroundColor = #colorLiteral(red: 0.4392156899, green: 0.01176470611, blue: 0.1921568662, alpha: 1)
When we run the method one by selecting a color on the attribute inspector, below is the result that’s created.
Advertisements
To Continue Learning Please Login
Login with Google