内部视图中使用autolayout以编程方式设置水平三个按钮

时间:2015-12-23 12:32:35

标签: ios swift uibutton autolayout

我不熟悉autolayout请帮助我, 在这里,我附上一个红色的图像,靠近" In" (右侧红色视图)我根据要设置按钮的文本添加了三个水平按钮(" #test","#general"," #Banter" )但问题是按钮之间的间距和当名称很大时,我的代码不能正常工作

 var setxPosition:CGFloat = 0
                var getwidth:CGFloat = cell.viewChannel!.frame.size.width
                var setxPosition2:CGFloat = getwidth/3
                setxPosition = -setxPosition2

                for object in cell.viewChannel!.subviews
                {

                    object.removeFromSuperview();
                }
                for var i = 0; i < getChannelname.count; i++ {

                        setxPosition = setxPosition + setxPosition2
                        var button   = UIButton.buttonWithType(UIButtonType.System) as! UIButton
                        button.frame = CGRectMake(setxPosition, 0, 100, 30)
                        button.backgroundColor = UIColor.clearColor()
                        button.tintColor = UIColor.blackColor()
                        button.setTitle("#\(getChannelname[i])" , forState: UIControlState.Normal)
                        button.titleLabel!.font = UIFont(name: "Arial", size: 12)


                        button.addTarget(self, action: "gotoChannel_byChannel:", forControlEvents: UIControlEvents.TouchUpInside)
                        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
                        cell.viewChannel!.addSubview(button)

                }

enter image description here

需要这样的解决方案

NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[Button1(Button2)]-5-[Button2]-10-|"
                                                                             options:NSLayoutFormatAlignAllCenterY
                                                                             metrics:nil
                                                                               views:views];

0 个答案:

没有答案