Ios Swift:如何以编程方式设置视图末尾的按钮

Ios Swift:如何以编程方式设置视图末尾的按钮,ios,swift,uiwebview,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 butto

在单击按钮时,我已通过编程方式设置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末尾设置按钮。

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

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)
}
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)

使用父视图的
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)

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

@IBVAR底部视图:UIView! self.bottamView.addSubview(btnTrvalRpt)btnTrvalRpt.translatesAutoResistingGMaskintoConstraints=false btnTrvalRpt.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive=true btnTrvalRpt.widthAnchor.constraint(equalToConstant:50).isActive=true btnTrvalRpt.heightAnchor.constraint(equalToConstant:50).isActive=true btnTrvalRpt.bottomAnchor.constraint(等式:self.view.bottomAnchor,常数:-20)。isActive=true-


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

@IBVAR底部视图:UIView! self.bottamView.addSubview(btnTrvalRpt)btnTrvalRpt.translatesAutoResistingGMaskintoConstraints=false btnTrvalRpt.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive=true btnTrvalRpt.widthAnchor.constraint(equalToConstant:50).isActive=true btnTrvalRpt.heightAnchor.constraint(equalToConstant:50).isActive=true btnTrvalRpt.bottomAnchor.constraint(等式:self.view.bottomAnchor,常数:-20)。isActive=true-


所说的“结尾”是指底部吗?是的,UIWebView的按钮尝试将按钮初始化更改为以下内容:
let button=UIButton(frame:cRect(x:webView.bounds.width/2-100,y:webView.bounds.height-100,width:100,height:50))
非常感谢:)通过“结尾”你的意思是在底部吗?是的,UIWebView的按钮尝试将按钮初始化更改为以下内容:
let button=UIButton(frame:cRect(x:webView.bounds.width/2-100,y:webView.bounds.height-100,width:100,height:50))
非常感谢:)@iron你应该改为
webView
,这是您案例中按钮的父视图。我给出了将来人们来这里的一般答案。@iron您应该改为使用
webView
,这是您案例中按钮的父视图。我给了人们将来来这里的一般答案。