当方向改变时,UIView不会调整大小

时间:2014-11-21 06:34:10

标签: ios swift xcode6

我使用以下函数制作自定义UIView。它可以在你开始的任何方向渲染,但一旦渲染,如果我改变了方向,它就不会正确调整大小。我在功能上做错了吗?

class func getRoundedView(text: String, view: UIView, color: UIColor, textColor: UIColor) -> UIView {

        var returnView : UIView = UIView(frame: CGRectMake(0, 0, view.frame.size.width, view.frame.size.height))

        var innerViewX : CGFloat = 0.1 * (returnView.frame.size.width)
        var innerViewY : CGFloat = 0.1 * (returnView.frame.size.height)
        var innerViewWidth : CGFloat = 0.8 * (returnView.frame.size.width)
        var innerViewHeight : CGFloat = 0.8 * (returnView.frame.size.height)
        var innerView : UIView = UIView(frame: CGRectMake(innerViewX, innerViewY, innerViewWidth, innerViewHeight))
        innerView.backgroundColor = color

        var corner : UIRectCorner = UIRectCorner.AllCorners
        var maskPath : UIBezierPath = UIBezierPath(roundedRect: innerView.bounds, byRoundingCorners: corner, cornerRadii: CGSizeMake(4.0, 4.0))
        var maskLayer : CAShapeLayer = CAShapeLayer()
        maskLayer.frame = innerView.bounds
        maskLayer.path = maskPath.CGPath
        innerView.layer.mask = maskLayer

        var label : UILabel = UILabel(frame: CGRectMake(0, 0, innerView.frame.size.width, innerView.frame.size.height))
        label.text = text
        label.center = CGPointMake(innerView.frame.size.width / 2.0, innerView.frame.size.height / 2.0)
        label.textColor = textColor
        label.textAlignment = NSTextAlignment.Center
        label.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin

        innerView.addSubview(label)
        innerView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin

        returnView.addSubview(innerView)
        returnView.backgroundColor = UIColor.clearColor()
        returnView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin

        return returnView

    }

0 个答案:

没有答案