Swift:如何在View编程结束时设置按钮

时间:2017-04-06 15:22:33

标签: ios swift uiwebview

点击按钮,我已经以编程方式设置了UIWebView。

if(button.tag = 1)
{
    let webV:UIWebView = UIWebView(frame: CGRectMake(10, 80, (UIScreen.mainScreen().bounds.width)-20, (UIScreen.mainScreen().bounds.height)/2))
    webView.loadHTMLString(html!, baseURL: nil)

let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
  button.backgroundColor = .greenColor()
  button.setTitle("Test Button", forState: .Normal)
  button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
  webView.addSubview(button)
self.view.addSubview(webV)
}

我尝试在webView末尾设置按钮,但我无法设置。我尝试了很多次,但我还在进行中。我想在我以编程方式创建的webView结尾处设置按钮。

2 个答案:

答案 0 :(得分:2)

使用父视图的bounds属性来定位子视图。

let bottomMargin = CGFloat(50) //Space between button and bottom of the screen
let buttonSize = CGSize(width: 100, height: 50)

let button = UIButton(frame: CGRect(
        x: 0, y: 0, width: buttonSize.width, height: buttonSize.height
    ))

button.center = CGPoint(x: parentView.bounds.size.width / 2,
                        y: parentView.bounds.size.height - buttonSize.height / 2 - bottomMargin)

答案 1 :(得分:0)

如何以编程方式在视图末尾设置按钮

@IBOutlet弱var bottomView:UIView! self.bottamView.addSubview(btnTrvalRpt)btnTrvalRpt.translatesAutoresizingMaskIntoConstraints = false btnTrvalRpt.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true btnTrvalRpt.widthAnchor.Constraint(equalt = 50%) (equalToConstant:50).isActive = true btnTrvalRpt.bottomAnchor.constraint(equalTo:self.view.bottomAnchor,常数:-20).isActive = true –

enter image description here