Prompt Title: Add a overlaying button on a modal using UIKit

Created 1 year ago
Votes 0
Views 206 times
IT
0

Prompt Detail:

To overlay a button over a modal on iOS using UIKit without using SwiftUI or Storyboards, you can use the following approach:

  1. Create the modal view controller and add the modal content as a subview. Here's an example of how to create a simple modal view controller:
less
class ModalViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Add the modal content as a subview let modalContent = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) modalContent.backgroundColor = .white modalContent.layer.cornerRadius = 10 view.addSubview(modalContent) // Center the modal content modalContent.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ modalContent.centerXAnchor.constraint(equalTo: view.centerXAnchor), modalContent.centerYAnchor.constraint(equalTo: view.centerYAnchor), modalContent.widthAnchor.constraint(equalToConstant: 200), modalContent.heightAnchor.constraint(equalToConstant: 200) ]) } }
  1. Add a button to the main view controller, and set its target to show the modal view controller:
less
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Add a button let button = UIButton(type: .system) button.setTitle("Show Modal", for: .normal) button.addTarget(self, action: #selector(showModal), for: .touchUpInside) view.addSubview(button) // Position the button button.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ button.centerXAnchor.constraint(equalTo: view.centerXAnchor), button.centerYAnchor.constraint(equalTo: view.centerYAnchor) ]) }
avatar
Bastian
Shared 1 prompt
Created 1 year ago

Leave a Comment

Related Tag Prompts

0
0
iOS App in Chinese
1 year ago 2023-04-21 14:54:30 Bruce
0
0
Linking iPhone and Arduino.
1 year ago 2023-04-22 09:46:21 Ali
0
0
Button Sound Issue.
1 year ago 2023-04-23 11:30:20 Flip
0
0
0
0
strategy
8 months ago 2024-02-22 08:39:58 Sam