Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在创建的数组中隔开按钮?_Ios_Swift3 - Fatal编程技术网

Ios 如何在创建的数组中隔开按钮?

Ios 如何在创建的数组中隔开按钮?,ios,swift3,Ios,Swift3,我想做一个弹出菜单,它是一个UIView,包含一系列按钮,这些按钮应该并排存在。相反,尽管我动态地更改了center.x,但它们都是堆叠在一起的。这是我的代码片段: func createPopUpView(number: Int) -> UIView{ let popUpView: UIView = UIView() var buttons: [UIButton] = [UIButton]() let button: UIButton = UIButton(fra

我想做一个弹出菜单,它是一个UIView,包含一系列按钮,这些按钮应该并排存在。相反,尽管我动态地更改了center.x,但它们都是堆叠在一起的。这是我的代码片段:

func createPopUpView(number: Int) -> UIView{
    let popUpView: UIView = UIView()
    var buttons: [UIButton] = [UIButton]()
    let button: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 32, height:42))
    button.backgroundColor = UIColor.black
    button.layer.cornerRadius = 5
    for i in 0..<number {
        buttons.append(button)
        buttons[i].setTitle(String(i), for: .normal)
        buttons[i].center.x += 32
        popUpView.addSubview(buttons[i])
    }       
    return popUpView        
}
func createPopUpView(编号:Int)->UIView{
让popUpView:UIView=UIView()
变量按钮:[UIButton]=[UIButton]()
let按钮:UIButton=UIButton(帧:CGRect(x:0,y:0,宽度:32,高度:42))
button.backgroundColor=UIColor.black
button.layer.cornerRadius=5

对于0中的i..您需要在每次循环中创建一个新按钮。此外,在创建popUpView时需要提供一个帧

试着这样做:

func createPopUpView(number: Int) -> UIView{
    let popUpView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: number * 32, height: 42))
    for i in 0..<number {
        let button: UIButton = UIButton(frame: CGRect(x: i * 32, y: 0, width: 32, height:42))
        button.backgroundColor = UIColor.black
        button.layer.cornerRadius = 5
        button.setTitle(String(i), for: .normal)
        popUpView.addSubview(button)
    }
    return popUpView
}
func createPopUpView(编号:Int)->UIView{
让popUpView:UIView=UIView(帧:CGRect(x:0,y:0,宽度:number*32,高度:42))

对于0中的i..
let button:UIButton=UIButton(帧:CGRect(x:i*32,y:0,宽度:32,高度:42))
将其放入
for
循环中?您确定“按钮”是“堆叠在一起”的吗?扰流板:它们不是。你只有一个按钮。仅仅因为你重复地将它附加到数组中并不意味着你正在创建它的副本。