Creating a Basic UIButton Programmatically

Creating a Basic UIButton Programmatically

6 June 2024 Stephan Petzl Leave a comment Tech-Help

When developing your iOS app, you might need to create UIButtons programmatically rather than using Interface Builder. This guide will walk you through the process of creating a UIButton in Objective-C and Swift, setting its properties, and adding it to your view.

Creating a UIButton in Objective-C

To create a UIButton in Objective-C, follow these steps:

  • Initialize the UIButton with a specific type.
  • Set the target and action for the button.
  • Set the title and frame for the button.
  • Add the button to the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

;

;
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];

Creating a UIButton in Swift

For Swift, the steps are similar but with Swift syntax:

  • Initialize the UIButton.
  • Set the title, title color, and frame for the button.
  • Set the target and action for the button.
  • Add the button to the view.
let myButton = UIButton(type: .custom)
myButton.setTitle("Hi, Click me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
self.view.addSubview(myButton)

@objc func pressedAction(_ sender: UIButton) {
    print("you clicked on button \(sender.tag)")
}

Creating a UIButton in SwiftUI

In SwiftUI, creating a button is straightforward and involves using the Button view:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Button(action: {
                // handle button action here
            }) {
                Text("Your Button Name")
                    .foregroundColor(.white)
                    .padding(10)
                    .background(Color.blue)
                    .cornerRadius(5)
                    .shadow(radius: 5)
            }
        }
    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

Conclusion

Creating UIButtons programmatically can be a powerful way to dynamically build your user interface based on the app’s state or user interactions. Whether you are using Objective-C, Swift, or SwiftUI, the process involves initializing the button, setting its properties, and adding it to the view.

For mobile developers looking to streamline their testing process, Repeato offers an excellent solution. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain automated tests quickly and efficiently. By leveraging computer vision and AI, Repeato enables you to focus on developing your app rather than spending time on test creation and maintenance. Additionally, it allows non-technical colleagues or QAs to handle test automation, freeing up more time for developers to concentrate on enhancing the app’s features.

Explore more about how Repeato can assist you in your testing needs by visiting our blog and documentation.

Like this article? there’s more where that came from!