Background and goal
Goal: I want to rotate and flip a UITextView
. (Why: see my previous question)
Problem: If I do the transform directly on the UITextView
, the text layout gets messed up for some unknown reason.
Solution: Put the UITextView
in a UIView
container, and then do the transform on the container.
New problem: Auto Layout (or any sort of layout) on the rotated view becomes a major headache.
Proposed solution: Make a subclass of UIView
that acts as an additional container for the rotated and flipped UIView
. Auto Layout should then work on this custom view.
Current Problem
It works when everything first appears (UITextView
has yellow background):
but when there is an orientation change, the following happens (blue is the subclassed UIView
background, set in IB):
If I disable the rotationView.addSubview(textView)
line, then the rotation container view (red) repositions itself just fine, even on an orientation change:
So the problem must be about where I am adding the UITextView
. But how do I do it?
Code
class MongolTextView: UIView {
// properties
var rotationView: UIView!
var textView: UITextView!
// This method gets called if you create the view in the Interface Builder
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// This method gets called if you create the view in code
override init(frame: CGRect){
super.init(frame: frame)
self.setup()
}
override func awakeFromNib() {
super.awakeFromNib()
self.setup()
}
func setup() {
rotationView = UIView(frame: self.frame)
rotationView.backgroundColor = UIColor.redColor()
self.addSubview(rotationView)
textView = UITextView(frame: CGRectZero)
textView.backgroundColor = UIColor.yellowColor()
textView.text = "This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text "
}
override func layoutSubviews() {
super.layoutSubviews()
// set the size of the rotation container view
let width = self.bounds.width
let height = self.bounds.height
rotationView.frame = CGRect(origin: CGPoint(x: CGFloat(0), y: CGFloat(0)), size: CGSize(width: height, height: width))
textView.frame = rotationView.bounds // Problem lines here???
rotationView.addSubview(textView) // Problem lines here???
// rotate, translate, and flip the container view
var rotation = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
// the following translation repositions the top left corner at the origin of the superview
var translation = CGAffineTransformMakeTranslation((rotationView.bounds.height / 2)-(rotationView.bounds.width / 2), (rotationView.bounds.width / 2)-(rotationView.bounds.height / 2))
var rotationAndTranslation = CGAffineTransformConcat(rotation, translation)
var transformPlusScale = CGAffineTransformScale(rotationAndTranslation, CGFloat(-1), CGFloat(1))
rotationView.transform = transformPlusScale
}
}
If I can't get this to work...
Although I have currently run up against a wall here, my next plan is to override drawRect()
to do the transforms. This isn't my first choice, though, because performance is supposedly slowed down by doing this.
width
andheight
ofself.bounds
stays the same. Good point about the transformation being applied twice. I'm not really sure how to fix this, though. – Gillis