Swift 3:如何在UIWeb视图上添加按钮

时间:2016-11-18 03:40:32

标签: swift xcode uiwebview

我正在做一个项目,我想在webview上添加6个按钮,但每当我尝试添加按钮时,应用程序都不会显示此按钮。

任何建议,帮助真的很感激!

由于

2 个答案:

答案 0 :(得分:0)

试试这个 -

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
    button.backgroundColor = UIColor.redColor()
    button.addTarget(self, action: #selector(ratingButtonTapped), forControlEvents: .TouchUpInside)
    self.profileVIew.addSubview(button)

func ratingButtonTapped(){
    print("Button pressed")
}

答案 1 :(得分:0)

我将在不使用故事板的情况下向您展示如何在代码中执行此操作。将他放入视图控制器类

 var setupButton: UIButton = {
            let button = UIButton(type: .system)
            button.backgroundColor = UIColor(r: 80, g: 101, b: 161, a: 256)
            button.setTitle("Register", for: .normal)
            button.setTitleColor(UIColor.white, for: .normal)
            button.translatesAutoresizingMaskIntoConstraints = false
            button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)

            button.addTarget(self, action: #selector(handleRegister), for: .touchUpInside)
            return button

        }()

然后在 viewDidLoad()函数中放置

view.addSubview(setupButton)
myButton()

然后在viewDidLoad()之外放置

func myButton() {

setupButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
setupButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
setupButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
setupButton.heightAnchor.constraint(equalToConstant: 150).isActive = true }


func handleRegister {
      #your action here 
}