I have a tableView with 3 Static Cells, each cell contains a subview with labels displaying the desired text, what I want to accomplish is to have the orientation programmatically rotate from Portrait to Landscape when the user taps on any of the 3 subviews.
I got it working for the first subview. However, when I try to add the same Tap gesture recognizer to the other subviews it still only works for one subview. Please advise, what am I missing? Thanks for any input.
Portrait:
Landscape
Table View Controller code:
import UIKit
class SampleTableViewController: UITableViewController {
@IBOutlet weak var newYorkViewWrapper: UIView!
@IBOutlet weak var sanFranciscoViewWrapper: UIView!
@IBOutlet weak var chicagoViewWrapper: UIView!
//Vars
let tapRec = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
print("Sample view rendered.")
//Tap gesture
tapRec.addTarget(self, action: "tappedView")
newYorkViewWrapper.addGestureRecognizer(tapRec)
newYorkViewWrapper.userInteractionEnabled = true
sanFranciscoViewWrapper.addGestureRecognizer(tapRec)
sanFranciscoViewWrapper.userInteractionEnabled = true
chicagoViewWrapper.addGestureRecognizer(tapRec)
chicagoViewWrapper.userInteractionEnabled = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//--------------------------------------------------------
// MARK: Hide status bar
//--------------------------------------------------------
override func prefersStatusBarHidden() -> Bool {
return true
}
func tappedView(){
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
//--------------------------------------------------------
// MARK: View Orientation method
//--------------------------------------------------------
override func shouldAutorotate() -> Bool {
return true
}
}