create a view with a clickable person logo at the header on the right, and three clickable buttons in the footer in swift

To create a view with a clickable person logo at the header and three clickable buttons in the footer in Swift, you can use the following steps:

  1. Create a new UIView file called CustomView. This will be the view that contains the header and footer.
  2. Inside the CustomView file, create two subviews: headerView and footerView.
  3. Add the person logo as a UIButton to the top-right corner of the headerView.
  4. Add three UIButton s to the footerView, and make them horizontally spaced across the width of the footerView.
  5. Add targets to the buttons so that they trigger specific actions when tapped.

Here's how you can implement it in code:

main.swift
class CustomView: UIView {

    let headerView = UIView()
    let footerView = UIView()

    let personButton = UIButton(type: .custom)
    let buttonA = UIButton()
    let buttonB = UIButton()
    let buttonC = UIButton()

    func setupView() {
        headerView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 50)
        footerView.frame = CGRect(x: 0, y: self.frame.height - 50, width: self.frame.width, height: 50)
        headerView.backgroundColor = .gray
        footerView.backgroundColor = .lightGray

        personButton.frame = CGRect(x: headerView.frame.width - 50, y: 0, width: 50, height: 50)
        personButton.setImage(UIImage(named: "person-logo"), for: .normal)

        buttonA.frame = CGRect(x: 0, y: 0, width: footerView.frame.width/3, height: footerView.frame.height)
        buttonB.frame = CGRect(x: footerView.frame.width/3, y: 0, width: footerView.frame.width/3, height: footerView.frame.height)
        buttonC.frame = CGRect(x: footerView.frame.width/3*2, y: 0, width: footerView.frame.width/3, height: footerView.frame.height)

        buttonA.setTitle("Button A", for: .normal)
        buttonB.setTitle("Button B", for: .normal)
        buttonC.setTitle("Button C", for: .normal)

        buttonA.addTarget(self, action: #selector(buttonATapped), for: .touchUpInside)
        buttonB.addTarget(self, action: #selector(buttonBTapped), for: .touchUpInside)
        buttonC.addTarget(self, action: #selector(buttonCTapped), for: .touchUpInside)

        headerView.addSubview(personButton)
        footerView.addSubview(buttonA)
        footerView.addSubview(buttonB)
        footerView.addSubview(buttonC)

        self.addSubview(headerView)
        self.addSubview(footerView)
    }

    @objc func buttonATapped() {
        print("Button A tapped")
    }
    @objc func buttonBTapped() {
        print("Button B tapped")
    }
    @objc func buttonCTapped() {
        print("Button C tapped")
    }
}
1950 chars
51 lines

Then, you can use this CustomView inside of another view by instantiating it in code or using it in a Storyboard. For example:

main.swift
let customView = CustomView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
view.addSubview(customView)
108 chars
3 lines

This will create a CustomView that is 320 pixels wide and 568 pixels tall (the size of an iPhone 5/SE screen), and add it to the view of the current view controller.

gistlibby LogSnag