添加按钮以编程方式查看创建的内容

时间:2019-06-20 16:11:05

标签: ios swift uibutton

我有一个按如下程序创建的视图

  let viewOne = UIView()
        viewOne.contentMode = .scaleAspectFit
        viewOne.backgroundColor = UIColor.blue
        viewOne.layer.masksToBounds = true
        viewOne.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(viewOne)

    NSLayoutConstraint.activate([
        viewOne.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        viewOne.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        viewOne.heightAnchor.constraint(equalToConstant:200),
        viewOne.widthAnchor.constraint(equalToConstant:200)])

我有以下按程序创建的以下按钮,

    let button = UIButton()
    button.frame = CGRect(x: 125, y: 125, width: 100, height: 50)
    button.backgroundColor = UIColor.green
    button.setTitle("Ok", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    viewOne.addSubview(button)

如何将button添加到viewOne的中心。预先感谢。

3 个答案:

答案 0 :(得分:1)

您可以尝试

let viewOne = UIView()
viewOne.contentMode = .scaleAspectFit
viewOne.backgroundColor = UIColor.blue
viewOne.layer.masksToBounds = true
viewOne.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(viewOne)

let button = UIButton() 
button.backgroundColor = UIColor.green
button.setTitle("Ok", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) 
button.translatesAutoresizingMaskIntoConstraints = false
viewOne.addSubview(button)

NSLayoutConstraint.activate([

    viewOne.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    viewOne.centerYAnchor.constraint(equalTo: view.centerYAnchor),
    viewOne.heightAnchor.constraint(equalToConstant:200),
    viewOne.widthAnchor.constraint(equalToConstant:200),

    button.centerXAnchor.constraint(equalTo: viewOne.centerXAnchor),
    button.centerYAnchor.constraint(equalTo: viewOne.centerYAnchor),  
    button.heightAnchor.constraint(equalToConstant:100),
    button.widthAnchor.constraint(equalToConstant:50)

])

提示:

您可以选择删除这两个约束,因为默认情况下按钮具有固有的内容大小,但是如果您需要固定的内容,请保留它们

 button.heightAnchor.constraint(equalToConstant:100),
 button.widthAnchor.constraint(equalToConstant:50)

答案 1 :(得分:0)

您可以使用自动布局,如下所示:

    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.backgroundColor = UIColor.green
    button.setTitle("Ok", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    viewOne.addSubview(button)
    NSLayoutConstraint.activate([
    button.centerXAnchor.constraint(equalTo: viewOne.centerXAnchor),
    button.centerYAnchor.constraint(equalTo: viewOne.centerYAnchor),
    button.heightAnchor.constraint(equalToConstant:50),
    button.widthAnchor.constraint(equalToConstant:100)])

或在进行一些计算之后手动放置按钮。

let button = UIButton(frame: CGRect(x: 50, y: 75, width: 100, height: 50))
viewOne.addSubview(button)

我获得了x和y,如下所示: x =视图一个宽度/ 2-按钮宽度/ 2 = 200/2-100/2 = 50 y = viewOne高度/ 2-按钮高度/ 2 = 200/2-50/2 = 75

答案 2 :(得分:0)

按钮本身具有宽度和高度。因此您无需修复

button.centerXAnchor.constraint(equalTo: viewOne.centerXAnchor)
button.centerYAnchor.constraint(equalTo: viewOne.centerYAnchor)