I'm done with the auto-layout stuff in my iOS universal App, and it's working perfectly in portrait. However, I want the user to be able to rotate the device and play the game in landscape mode. The problem I'm facing is that I don't want the layout to change at all, and only change the controls of the game (sliding up the screen should make the player go up in both orientations).
Thing is, I don't know how to prevent orientation from changing the layout and at the same time be able to change behaviour based on the orientation. Do you guys have any idea how I could manage that?
Did found a way to do, for future reference, when an orientation is disabled, we still can access device orientation (and not interface orientation), and register a notification to act upon change.
class ViewController: UIViewController {
var currentOrientation = 0
override func viewDidLoad() {
super.viewDidLoad()
// Register for notification about device orientation change
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(deviceDidRotate(notification:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
// Remove observer on window disappears
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
if UIDevice.current.isGeneratingDeviceOrientationNotifications {
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
}
// That part gets fired on orientation change, and I ignore states 0 - 5 - 6, respectively Unknown, flat up facing and down facing.
func deviceDidRotate(notification: NSNotification) {
if (UIDevice.current.orientation.rawValue < 5 && UIDevice.current.orientation.rawValue > 0) {
self.currentOrientation = UIDevice.current.orientation.rawValue
}
}
}
Related
This is a continuation of an earlier post. What I was wondering was how to add the user defaults for the dark mode throughout the app. Please do not pay attention for the code that says UserDefaults in my last post, I was following a tutorial and just kind of copied what he did, not knowing anything at all about User Defaults. The whole dark mode works beautifully throughout the app. I just need to know how to do all the user defaults. If you have any questions feel free to ask.
The code below is what the custom cell looks like below that is in a settings view controller, to change the app to a Dark Mode. Everything works great and as it should. I just need to put in the user defaults into the actions.
import UIKit
class DarkModeTableViewCell: UITableViewCell {
var DarkisOn = Bool()
let userDefaults = UserDefaults.standard
#IBOutlet var darkModeSwitchOutlet: UISwitch!
override func awakeFromNib() {
super.awakeFromNib()
NotificationCenter.default.addObserver(self, selector: #selector(darkModeEnabled(_:)), name: .darkModeEnabled, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(darkModeDisabled(_:)), name: .darkModeDisabled, object: nil)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
#IBAction func darkModeSwitched(_ sender: Any) {
if darkModeSwitchOutlet.isOn == true {
//enable dark mode
DarkisOn = true
//add a userDefault here so that the app will stay in dark mode
NotificationCenter.default.post(name: .darkModeEnabled, object: nil)
} else {
//enable light mode
DarkisOn = false
//add a userDefault here so that the app will stay in light mode
NotificationCenter.default.post(name: .darkModeDisabled, object: nil)
}
}
#objc private func darkModeEnabled(_ notification: Notification) {
DarkModeTableViewCellChange.instance.set(for: self)
textLabel?.textColor = UIColor.white
}
#objc private func darkModeDisabled(_ notification: Notification) {
LightModeTableViewCellChange.instance.set(for: self)
textLabel?.textColor = UIColor.black
}
}
EDIT: What I am looking for is how to add the user defaults to the dark mode. So once the dark mode is turned on, then when you close the app, it would stay on, etc.
Everything you do with NSUserDefaults is to store settings and retrieve them. You would store what theme your user is using in them.
So do something like this when changing your themes (in your previous question you were already doing something like this):
let defaults = UserDefaults.standard
// Do something like this when using changing your theme to dark mode.
defaults.set(true, "darkModeEnabled")
// Do something like this when changing your theme to your standard one
defaults.set(false, "darkModeEnabled")
In the viewWillAppear of your themable view controllers, you just check the value of the key you specified in UserDefaults.
/// Check if the user is using dark mode in viewDidLoad.
override func viewWillAppear() {
super.viewDidLoad()
let darkModeEnabled = defaults.bool(forKey: "darkModeEnabled")
if darkModeEnabled {
// Apply your dark theme
} else {
// Apply your normal theme.
}
}
This way your app your view will controllers will have the right theme upon loading, and the user will see the right one when loading the app.
Recommended reading: UserDefaults
As an aside note, the tutorial series you are following on YouTube is clearly not good enough for beginners, as it can be evidenced by the fact it mentions UserDefaults and even uses them but apparently never tells you how to use them. You should just get a good book on iOS development instead.
Is it possible to move the keyboard up so it doesn't cover the UITabViewController's TabBar?
Update after being given more context in comments
If your main concern is letting the user dismiss the keyboard, there are some well known patterns that are commonly applied on the platform:
Assumption regarding UI (derived from your comment):
- UITableView as main content
To make the keyboard dismissible, you can utilise a property on UIScrollView called .keyboardDismissMode. (UITableView is derived from UIScrollView, so it inherits the property.)
The default value for this property is .none. Change that to either .onDrag or .interactive. Consult the documentation for differences between the latter two options.
Behind the scenes, UIKit sets up a connection between the UIScrollView instance and any incoming keyboard. This allows the user to "swipe away" the keyboard by interacting with the scroll view.
Note that in order for this feature to work, your UIScrollView needs to be scrollable. To understand what 'scrollable' means in this context, please see this gist.
If your tableView has very few or no rows, it is likely not natively scrollable. To account for that, set tableView.alwaysBounceVertical = true. This will make sure your users can dismiss the keyboard regardless of the number of rows in the table.
Most of the popular apps handling keyboard dismissal also make it possible to dismiss the keyboard simply by tapping the content partially overlapped by it (in your case, the tableView). To enable this, you would simply have to install a UITapGestureRecognizer on your view and dismiss the keyboard in its action method:
class MyViewController: UIViewController {
func viewDidLoad() {
super.viewDidLoad()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tapRecognizer)
}
}
//MARK: - Tap handling
fileprivate extension MyViewController {
#objc func handleTap() {
if searchBar.isFirstResponder {
searchBar.resignFirstResponder()
}
// Alternative
// view.endEditing(true)
}
}
// -
Old answer
Yes, you can actually do this without using private API.
Disclaimer
You should really think about whether you actually want to do this. Opening the keyboard in virtually every use case should create a new "context" of editing which modally "blocks" other contexts (such as the navigation context provided by UITabBarController and its UITabBar). I guess one could make the point that users are able to leave an editing context by interacting with a potentially present UINavigationBar which is usually not blocked by keyboards. However, this is a known interaction throughout the system. Not blocking a UITabBar or UIToolbar while showing the keyboard on the other hand, is not. That being said, use the code below to move the keyboard up, but critically review the UX you are creating. I'm not to say it does never make sense to move the keyboard up, but you should really know what you're doing here. To be honest, it also looks kind of iffy, having the keyboard float above the tab bar.
Code
extension Sequence {
func last(where predicate: (Element) throws -> Bool) rethrows -> Element? {
return try reversed().first(where: predicate)
}
}
// Using `UIViewController` as an example. You could and actually should factor this logic out.
class MyViewController: UIViewController {
deinit {
NotificationCenter.default.removeObserver(self)
}
func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
}
}
//MARK: - Keyboard handling
extension MyViewController {
private var keyboardOffset: CGFloat {
// Using a fixed value of `49` here, since that's what `UITabBar`s height usually is.
// You should probably use something like `-tabBarController?.tabBar.frame.height`.
return -49
}
private var keyboardWindowPredicate: (UIWindow) -> Bool {
return { $0.windowLevel > UIWindowLevelNormal }
}
private var keyboardWindow: UIWindow? {
return UIApplication.shared.windows.last(where: keyboardWindowPredicate)
}
#objc fileprivate func keyboardWillShow(notification: Notification) {
if let keyboardWindow = keyboardWindow {
keyboardWindow.frame.origin.y = keyboardOffset
}
}
#objc fileprivate func keyboardWillHide(notification: Notification) {
if let keyboardWindow = keyboardWindow {
keyboardWindow.frame.origin.y = 0
}
}
}
// -
Caution
Note that if you are using the .UIKeyboardWillShow and .UIKeyboardWillHide notifications to account for the keyboard in your view (setting UIScrollView insets, for example), you would have to also account for any additional offset by which you move keyboard window.
This works and is tested with iOS 11. However, there is no guarantee that the UIKit team won't change the order of windows or something else that breaks this in future releases. Again, you are not using any private API, so AppStore review should not be in danger, but you are doing something that you're not really supposed to do with the framework, and that can always come around and bite you later on.
I subclassed UICollectionViewLayout in order to create a calendar. I would like to give users the ability to change some settings like the number of days to be shown on the screen (7 by default).
I save daysToShow in UserDefaults. Whenever the UIStepper value is changed it calls this method:
func stepperValueChanged(sender:UIStepper){
stepperValue = String(Int(sender.value))
valueLabel.text = String(Int(sender.value))
UserDefaults.standard.set(String(Int(sender.value)), forKey: "daysToShow")
NotificationCenter.default.post(name: Notification.Name(rawValue: "calendarSettingsChanged"), object: nil, userInfo: nil)
}
So after I save the new value in UserDefault, I post a notification which then calls reloadForSettingsChange (which is actually getting called as I set a breakpoint here):
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reloadForSettingsChange(notification:)), name: NSNotification.Name(rawValue: "calendarSettingsChanged"), object: nil)
// other code.....
}
func reloadForSettingsChange(notification:NSNotification){
// here I save the user setting in a variable declared in my custom UICollectionViewLayout
self.calendarView.daysToShow = Int(UserDefaults.standard.string(forKey: "daysToShow")!)
self.calendarView.daysToShowOnScreen = Int(UserDefaults.standard.string(forKey: "daysToShow")!)
self.calendarView.forceReload(reloadEvent: true)
}
func forceReload(reloadEvent:Bool){
DispatchQueue.main.async {
if reloadEvent{
self.groupEventsByDays()
self.weekFlowLayout?.invalidateCacheLayout()
self.collectionView.reloadData()
}
}
}
func invalidateCacheLayout(){
self.needsToPopulateAttributesForAllSections = true
self.cachedDayDateComponents?.removeAllObjects()
self.cachedStartTimeDateComponents?.removeAllObjects()
self.cachedEndTimeDateComponents?.removeAllObjects()
self.cachedCurrentDateComponents?.removeAllObjects()
self.cachedEarliestHour = Int.max
self.cachedLatestHour = Int.min
self.cachedMaxColumnHeight = CGFloat.leastNormalMagnitude
self.cachedColumnHeights?.removeAllObjects()
self.cachedEarliestHours?.removeAllObjects()
self.cachedLatestHours?.removeAllObjects()
self.itemAttributes?.removeAllObjects()
self.allAttributes?.removeAllObjects()
_layoutAttributes.removeAll()
self.invalidateLayout()
}
The problem is that the layout is not being updated (invalidated?) until I rotate the screen device (i.e from landscape to portrait). The function I use for the rotation calls exactly the same method I call in reloadForSettingsChange so I don't understand why it works when I rotate the screen and not before:
func rotated() {
switch UIDevice.current.orientation {
case .landscapeLeft, .landscapeRight:
calendarView.forceReload(reloadEvent: true)
default:
calendarView.forceReload(reloadEvent: true)
}
}
I found the solution: I am now calling setNeedsLayout():
func reloadForSettingsChange(notification:NSNotification){
self.calendarView.daysToShow = Int(UserDefaults.standard.string(forKey: "daysToShow")!)
self.calendarView.daysToShowOnScreen = Int(UserDefaults.standard.string(forKey: "daysToShow")!)
self.calendarView.forceReload(reloadEvent: true)
self.calendarView.setNeedsLayout() // this line has been added
}
I had initially used layoutSubviews() which worked but Apple Documentation says:
You should not call this method directly. If you want to force a
layout update, call the setNeedsLayout() method instead to do so prior
to the next drawing update. If you want to update the layout of your
views immediately, call the layoutIfNeeded() method.
I tried setNeedsLayout() which seemed to be the right method to call (?!?) but since it wasn't working I used setNeedsLayout():
Call this method on your application’s main thread when you want to
adjust the layout of a view’s subviews. This method makes a note of
the request and returns immediately. Because this method does not
force an immediate update, but instead waits for the next update
cycle, you can use it to invalidate the layout of multiple views
before any of those views are updated. This behavior allows you to
consolidate all of your layout updates to one update cycle, which is
usually better for performance.
How can I just rotate the buttons when my device is in portrait or landscape mode? I know something like that:
button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
But where I have to call it up in my code?
I don't want to set my device in landscape mode, I just want to rotate the icons when it should be in landscape mode.
Thanks in advance!
You should override func
viewWillTransitionToSize(_ size: CGSize, withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
And call transform there
Don't forget to assign CGAffineTransformIdentity when rotated back
More about rotation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIContentContainer_Ref/index.html#//apple_ref/occ/intfm/UIContentContainer/viewWillTransitionToSize:withTransitionCoordinator:
The best way to do this is viewDidLoad add this line of code bellow:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rotate), name: UIDeviceOrientationDidChangeNotification, object: nil)
and then in function rotate in case of device orientation do some code like this:
func rotate(){
if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
//your code here in landscape mode
}
if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
//your code in portrait mode
}
}
this solution is simple and really easy to do.
I guess you know how to "rotate" the buttons already, so I'll just tell you how to know that the device has rotated.
In a view controller, override willRotateToInterfaceOrientation:
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
In the method body, you can check the value of toInterfaceOrientation to know which orientation the device is rotating to. For example:
switch toInterfaceOrientation {
case Portrait:
// some code here...
default:
break
}
Previously if one presented a keyboard on one's own app one would embed everything in a UIScrollView and adjust the contentInset to keep content from being obscured by the keyboard.
Now with split view multitasking on iOS 9 the keyboard may appear at any moment and stay visible even while the user is no longer interacting with the other app.
Question
Is there an easy way to adapt all view controllers that were not expecting the keyboard to be visible and without start embedding everything in scrollviews?
The secret is to listen to the UIKeyboardWillChangeFrame notification that is triggered whenever the keyboard is shown/hidden from your app or from another app running side by side with yours.
I created this extension to make it easy to start/stop observing those events (I call them in viewWillAppear/Disappear), and easily get the obscuredHeight that is usually used to adjust the bottom contentInset of your table/collection/scrollview.
#objc protocol KeyboardObserver
{
func startObservingKeyboard() // Call this in your controller's viewWillAppear
func stopObservingKeyboard() // Call this in your controller's viewWillDisappear
func keyboardObscuredHeight() -> CGFloat
#objc optional func adjustLayoutForKeyboardObscuredHeight(_ obscuredHeight: CGFloat, keyboardFrame: CGRect, keyboardWillAppearNotification: Notification) // Implement this in your controller and adjust your bottom inset accordingly
}
var _keyboardObscuredHeight:CGFloat = 0.0;
extension UIViewController: KeyboardObserver
{
func startObservingKeyboard()
{
NotificationCenter.default.addObserver(self, selector: #selector(observeKeyboardWillChangeFrameNotification(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
func stopObservingKeyboard()
{
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
func observeKeyboardWillChangeFrameNotification(_ notification: Notification)
{
guard let window = self.view.window else {
return
}
let animationID = "\(self) adjustLayoutForKeyboardObscuredHeight"
UIView.beginAnimations(animationID, context: nil)
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey]! as AnyObject).intValue)!)
UIView.setAnimationDuration((notification.userInfo![UIKeyboardAnimationCurveUserInfoKey]! as AnyObject).doubleValue)
let keyboardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue
_keyboardObscuredHeight = window.convert(keyboardFrame!, from: nil).intersection(window.bounds).size.height
let observer = self as KeyboardObserver
observer.adjustLayoutForKeyboardObscuredHeight!(_keyboardObscuredHeight, keyboardFrame: keyboardFrame!, keyboardWillAppearNotification: notification)
UIView.commitAnimations()
}
func keyboardObscuredHeight() -> CGFloat
{
return _keyboardObscuredHeight
}
}