如何旋转UISegmentedControl并增加大小以适应任何数量的文本

时间:2016-08-25 16:37:02

标签: ios swift uisegmentedcontrol

我一直在尝试使用UISegmentedControl作为多项选择工具。我想如果我能将它旋转90度(以及里面的文字)并扩展细胞,它将是一个非常流畅的工具。但无论我尝试什么,它都会不断给我这个broken UISegmentedControl

的变体

顺便说一下,这是在UICollectionView中。这是我的UICollectionViewCell代码

class customCollectionCell: UICollectionViewCell {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var questionDescriptionLabel: UILabel!
@IBOutlet weak var viewForSegment: UIView!
var segment: CustomSegmentedControl!

func heightForView(text:String, width:CGFloat) -> CGFloat{
    let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.text = text

    label.sizeToFit()
    return label.frame.height
}

func loadQuestions(choices: [String], pointIndex: [Int]) {
    segment = CustomSegmentedControl(items: choices)
    segment.transform = CGAffineTransformMakeRotation(CGFloat(M_PI / 2.0));
    for view in segment.subviews {
        for sv in view.subviews {
            if sv.isKindOfClass(UILabel){
                let subview: UILabel = sv as! UILabel
                subview.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI / 2.0))
                subview.addConstraint(NSLayoutConstraint(item: subview,
                    attribute: NSLayoutAttribute.Height,
                    relatedBy: NSLayoutRelation.Equal,
                    toItem: nil,
                    attribute:NSLayoutAttribute.NotAnAttribute,
                    multiplier:1,
                    constant: heightForView(subview.text!, width: self.viewForSegment.frame.width)))
                subview.addConstraint(NSLayoutConstraint(item: subview,
                    attribute: NSLayoutAttribute.Width,
                    relatedBy: NSLayoutRelation.Equal,
                    toItem: nil,
                    attribute:NSLayoutAttribute.NotAnAttribute,
                    multiplier:1,
                    constant: self.viewForSegment.frame.width))
            }
        }
    }
    segment.pointIndex = pointIndex
    /*
    self.addConstraint(NSLayoutConstraint(item: segment, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.LeftMargin, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: segment, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: segment, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.RightMargin, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: segment, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: questionDescriptionLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
    segment.frame = CGRect(origin: CGPoint(x: questionLabel.x, y: ), size: segment.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize))
    */

    segment.bounds.size = segment.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize)
    viewForSegment.addSubview(segment)
    viewForSegment.frame.size = viewForSegment.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize)
}

}

2 个答案:

答案 0 :(得分:0)

您可以使用CGAffineTransformMakeRotation(M_PI_2)将控件旋转90度,
并在每个细分上CGAffineTransformMakeRotation(-M_PI_2)将其旋转回来 然后,您需要计算得到的帧/边界。

// rotate view
self.transform = newValue ? CGAffineTransformMakeRotation(CGFloat(M_PI_2)) : CGAffineTransformIdentity

let padding : CGFloat = 8
let maxSize = CGSize(width: CGFloat.max, height: CGFloat.max)
var size : CGSize
var maxHeight : CGFloat = 0
var totalWidth : CGFloat = 0

// reverse rotate segment content
for segment in subviews {
    for contentView in segment.subviews {
        size = contentView.sizeThatFits(maxSize)
        maxHeight = max(maxHeight, newValue ? size.width : size.height)
        totalWidth += (newValue ? size.height : size.width) + padding
        contentView.transform = newValue ? CGAffineTransformMakeRotation(-CGFloat(M_PI_2)) : CGAffineTransformIdentity
    }
}

// adjust size
bounds = CGRect(x: 0, y: 0, width: totalWidth + padding, height: maxHeight + 2*padding)

随意使用我的UISegmentedControl.vertical扩展程序: https://gist.github.com/yonat/18261fd04cd11a3e9959835509b312c2

答案 1 :(得分:0)

你可以通过这样做来实现

segmentControl.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)