gpt4 book ai didi

ios - 在同一行的页脚中添加两个按钮

转载 作者:行者123 更新时间:2023-11-30 13:19:10 25 4
gpt4 key购买 nike

你好,我想以编程方式在我的 ViewController 上添加两个按钮,如下所示 enter image description here

我所做的就是这个

func createButton(buttonTitle: String,buttonAction: Selector) -> UIButton{
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame)/2, 48)
// button.translatesAutoresizingMaskIntoConstraints = false

button.setTitle(buttonTitle, forState: UIControlState.Normal)
button.addTarget(self, action:buttonAction, forControlEvents: UIControlEvents.TouchUpInside)
button.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
button.titleLabel?.font = UIFont(name: Variables.MONTESERRAT_REGULAR, size: 20.0)



button.backgroundColor = UIColor().blueColor() //top
button.titleEdgeInsets = UIEdgeInsetsMake(0.0,10.0, 10.0, 0.0)
return button
}



override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

let footerView = UIView()
footerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 48)

bottomButton = createButton("PUBLISH", buttonAction: "Publish")

footerView.addSubview(bottomButton!)



return footerView

}

目前它只打印一个按钮。对于第二个按钮我正在这样做

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

let footerView = UIView()
footerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 48)

bottomButton = createButton("PUBLISH", buttonAction: "Publish")

bottomButton2 = createButton("Delete", buttonAction: "Delete")

footerView.addSubview(bottomButton!)
footerView.addSubview(bottomButton2!)




return footerView

}

此代码未打印两个按钮。只打印一个

最佳答案

我认为问题在于两个按钮的位置相同,因此一个按钮显示在另一个按钮之上。

试试这个:

    func createButton(buttonTitle: String,buttonAction: Selector, buttonNumber : Int) -> UIButton{

let backgroundColor = buttonNumber == 0 ? UIColor.blueColor() : UIColor.orangeColor()
let space : CGFloat = 5

let button = UIButton(type: UIButtonType.System) as UIButton
let width = (CGRectGetWidth(self.view.frame) - space)/2

let xPos = buttonNumber == 0 ? 0 : width + space
button.frame = CGRectMake(xPos, 0, width, 48)
// button.translatesAutoresizingMaskIntoConstraints = false


button.setTitle(buttonTitle, forState: UIControlState.Normal)
button.addTarget(self, action:buttonAction, forControlEvents: UIControlEvents.TouchUpInside)
button.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
// button.titleLabel?.font = UIFont(name: Variables.MONTESERRAT_REGULAR, size: 20.0)

button.backgroundColor = backgroundColor
button.titleEdgeInsets = UIEdgeInsetsMake(0.0,10.0, 10.0, 0.0)
return button
}

然后像这样添加它:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

let footerView = UIView()
footerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 48)

bottomButton = createButton("PUBLISH", buttonAction: "Publish", buttonNumber: 0)

bottomButton2 = createButton("Delete", buttonAction: "Delete", buttonNumber: 1)

footerView.addSubview(bottomButton!)
footerView.addSubview(bottomButton2!)

return footerView

}

关于ios - 在同一行的页脚中添加两个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37933631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com