如何在空UIview上设置UIButton

时间:2016-01-04 05:39:53

标签: ios swift uiview uibutton

我已经在UIView上创建了多个UIView和添加和删除按钮 但我删除第二个索引上的按钮然后添加新按钮后,新按钮覆盖零索引像这样

我创建了4个视图,每个视图包含一个按钮

我的问题是当我从特定视图中删除按钮然后删除按钮但是当我再次在同一视图上添加按钮时,按钮添加第一个视图而不是相同的视图

enter image description here

所以如何在空的UIView上添加按钮

var width : CGFloat = 30
    func buttons() {

        answer =  ArrAnswer.objectAtIndex(k) as! String
        let letters : String = answer as String

        print(answer)
        strFinal = randomize(letters as String)

        for(i=0; i<strFinal?.characters.count; i++) {

            btn = UIButton()
            btn.frame = CGRectMake(width-10, 45, 40, 40)
            btn.tag = i
            print("btn TAG is : \(btn.tag)")
            ins = strFinal?.substringWithRange(i, location: 1)
            btn.setTitle(ins, forState: UIControlState.Normal)

            btn.layer.cornerRadius = 0.2 * btn.bounds.size.width
            btn.backgroundColor = UIColor.whiteColor()
            btn.titleLabel!.textAlignment = .Center
            btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
            btn.titleLabel?.font = UIFont.boldSystemFontOfSize(20)

            btn.addTarget(self, action: "button_click:", forControlEvents: UIControlEvents.TouchUpInside)
            width = width + 50
            if(i > 5) {
                if(i == 6) {
                    width = 40
                }

                btn.frame = CGRectMake(width-20, 95, 40, 40)
                width = width + 0
            }
            if(i > 11) {
                if(i == 12) {
                    width = 40
                }
                btn.frame = CGRectMake(width-20, 145, 40, 40)
                width = width + 0
            }

            answerButton .insert(btn, atIndex: i)
            //print(answerButton)
            viewButton.addSubview(btn)
        }
    }

    var btnwidth : CGFloat = 10

    func button_click(sender: UIButton) {

        for(i=0; i<strFinal?.characters.count; i++ ) {

            if sender.tag == i {
                sender.hidden = true

                button = UIButton(type: UIButtonType.System) as UIButton


                button.frame = CGRectMake(0, 0, 30, 30)

                ins = strFinal?.substringWithRange(i, location: 1)
                button.setTitle(ins, forState: UIControlState.Normal)

                button.tag = i

                print("new button Tag: \(button.tag)")
                Arrtemp.addObject(ins!)
                print("button TAG : \(button.tag)")
                button.backgroundColor = UIColor.whiteColor()
                button.layer.cornerRadius = 0.2 * btn.bounds.size.width
                button.setTitleColor(UIColor.blackColor(), forState: .Normal)
                button.titleLabel?.font = UIFont.boldSystemFontOfSize(20)

                //Spring Animation
                button.transform = CGAffineTransformMakeScale(0.1, 0.1)
                UIView.animateWithDuration(2.0,
                    delay: 0,
                    usingSpringWithDamping: 0.2,
                    initialSpringVelocity: 6.0,
                    options: UIViewAnimationOptions.AllowUserInteraction,
                    animations: {
                        self.button.transform = CGAffineTransformIdentity
                    }, completion: nil)

                button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)


                arrWidth[check].addSubview(button)

                check++

            }
        }
var viewwidth : CGFloat = 10
    func answerView() {

        let commonSpacing : CGFloat = 10
        let spaceSpacing : CGFloat = 35

        for(i=0; i<answer.characters.count; i++) {

            ansview = UIView(frame: CGRectMake(viewwidth-5, 10, 30, 30))
            ansview.layer.cornerRadius = 0.2 * btn.bounds.size.width
            ansview.backgroundColor = UIColor.grayColor()
            ansview.tag = i;
            arrWidth.append(ansview)

            viewAnswer.addSubview(ansview)

            viewwidth = viewwidth + 35

            let currCharacter = answer?[i]
            print(currCharacter)
            var tempSpacing = commonSpacing
            if currCharacter ==  " " {

                tempSpacing = spaceSpacing
            }
            viewwidth = ansview.frame.maxX + tempSpacing
        }

    }

提前感谢...

0 个答案:

没有答案