incorrect vertical navigation items alignment - swift

I'm trying to customize Navigation bar and get it bigger with vertical alignment. Here is my code:
#IBOutlet weak var topNavTitleLabel: UILabel!
#IBOutlet weak var topNavStackView: UIStackView!
#IBOutlet weak var leftNavHomeButton: UIBarButtonItem!
#IBOutlet weak var rightNavSearchButton: UIBarButtonItem!
var rightNavSearchButton_: UIBarButtonItem?
#IBAction func searchAction(sender: UIButton) {
topNavStackView.hidden = true
topNavStackView.hidden = false
rightNavSearchButton_ = rightNavSearchButton
navigationItem.rightBarButtonItem = nil
}
#IBAction func performSearch(sender: UIButton) {
topNavStackView.hidden = false
topNavStackView.hidden = true
navigationItem.rightBarButtonItem = rightNavSearchButton_
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if let navConBar = self.navigationController?.navigationBar {
navConBar.bounds = CGRectMake(0, 0, view.bounds.width, 100)
navConBar.setTitleVerticalPositionAdjustment(-10.0, forBarMetrics: .Default)
let offset = UIOffset(horizontal: 0, vertical: -10)
navigationItem.rightBarButtonItem?.setTitlePositionAdjustment(offset, forBarMetrics: .Default)
navigationItem.leftBarButtonItem?.setTitlePositionAdjustment(offset, forBarMetrics: .Default)
}
}
When It appears it looks ok.
But When I click on right navigation bar button, left button goes to down.
Also when it turns back:
How can it be fixed?

Related

CAGradientLayer doesn't work and still showing solid color

I'm trying to add a gradient to a view but it doesn't work. This is the code:
class TermsAndConditionViewController: UIViewController, NavigationBarToggling {
#IBOutlet weak var tncContentView: UIView!
#IBOutlet weak var tncTextView: UITextView!
#IBOutlet weak var declineButton: AeonButton!
#IBOutlet weak var acceptButton: AeonButton!
#IBOutlet weak var unreadIndicatorView: UIView!
#IBOutlet weak var unreadLabel: UILabel!
let isNavigationBarHidden: Bool = false
let disposeBag = DisposeBag()
lazy var segueManager: SegueManager = {
return SegueManager(viewController: self)
}()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segueManager.prepare(for: segue)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupViews()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
title = "Terms & Conditions"
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupGradientUnreadIndicator()
}
private func setupViews() {
tncContentView.addCorner(radius: 8)
tncContentView.addBorder(width: 1, color: UIColor.black.withAlphaComponent(0.1).cgColor)
}
private func setupGradientUnreadIndicator() {
let gradient = CAGradientLayer()
gradient.type = .axial
gradient.colors = [
R.color.vcSubBgColor()!.withAlphaComponent(0),
UIColor.white
]
gradient.locations = [0, 1]
gradient.startPoint = CGPoint(x: 1, y: 0)
gradient.startPoint = CGPoint(x: 1, y: 1)
gradient.frame = unreadIndicatorView.bounds
unreadIndicatorView.layer.insertSublayer(gradient, at: 0)
}
}
The result is this:
As you can see, the view at the bottom of the Text View is still solid. I was expecting it will go from transparent to white. Can you help me?
Thanks.
PS: BTW, the reason I call the setupGradientUnreadIndicator() in viewDidAppear() is because my whole scene is using auto-layout. So I put it there when the frame of the view is already calculated.
As mentioned by #Larme in the comment, it is because I used UIColor instead of CGColor in the gradient.colors. After I changed it to CGColor it worked fine now.

On pressed touch Hide/Show UIImage, and count number down from 10 to 0

On every pressed touch I want UILabel to count number down one by one from 10 to 0.
Also when pressed I want to show UIImageView: "MyRocketWith", and when release the pressed touch I want to show UIImageView: "MyRocket".
class ViewController: UIViewController {
#IBOutlet weak var MyRocket: UIImageView!
#IBOutlet weak var MyRocketWith: UIImageView!
#IBOutlet weak var Countdown: UILabel!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch: UITouch? = touches.first
if touch?.view != self.MyRocket {
self.MyRocketWith.isHidden = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
The below code shows and hides the UIImageView on click of UILabel
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var MyRocketWith: UIImageView!
#IBOutlet weak var CountDownLabel: UILabel!
#IBOutlet weak var MyRocket: UIImageView!
var count = 10
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
CountDownLabel.isUserInteractionEnabled = true
CountDownLabel.addGestureRecognizer(tap)
MyRocket.isHidden = false
MyRocketWith.isHidden = true
MyRocketWith.tag=0
}
#IBAction func tapFunction(sender: UITapGestureRecognizer) {
print("tap working")
count = count-1
CountDownLabel.text = String(count)
if MyRocketWith.tag == 0
{
MyRocket.isHidden = true
MyRocketWith.isHidden = false
MyRocketWith.tag=1
}
else
{
MyRocket.isHidden = false
MyRocketWith.isHidden = true
MyRocketWith.tag=0
}
}
}

Animation only works on one button (ripple view)

Site used: https://material.io/develop/ios/components/ripple/
Here is my code:
import MaterialComponents.MaterialRipple
class ViewController: UIViewController {
// let rippleView = MDCRippleView()
let rippleTouchController = MDCRippleTouchController()
#IBOutlet weak var playBtn: UIButton!
#IBOutlet weak var levelsBtn: UIButton!
#IBOutlet weak var topicsBtn: UIButton!
#IBOutlet weak var settingsBtn: UIButton!
#IBOutlet weak var instaBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//This works for instabtn (lastone)
// rippleTouchController.rippleView.rippleColor = .lightGray
// rippleTouchController.addRipple(to: playBtn)
// rippleTouchController.addRipple(to: levelsBtn)
// rippleTouchController.addRipple(to: topicsBtn)
// rippleTouchController.addRipple(to: settingsBtn)
// rippleTouchController.addRipple(to: instaBtn)
}
override func viewDidAppear(_ animated: Bool) {
// Currently only works for playbtn
rippleTouchController.rippleView.rippleColor = .lightGray
rippleTouchController.addRipple(to: levelsBtn)
rippleTouchController.addRipple(to: topicsBtn)
rippleTouchController.addRipple(to: settingsBtn)
rippleTouchController.addRipple(to: instaBtn)
rippleTouchController.addRipple(to: playBtn)
}
}
I've tried to put the code in viewDidAppear but it doesn't make a difference. Any thoughts?
Foreach button you have to create a MDCRippleTouchController.
let rippleTouchController = MDCRippleTouchController()
let rippleTouchController2 = MDCRippleTouchController()
let rippleTouchController3 = MDCRippleTouchController()
let rippleTouchController4 = MDCRippleTouchController()
let rippleTouchController5 = MDCRippleTouchController()
override func viewDidAppear(_ animated: Bool) {
rippleTouchController.rippleView.rippleColor = .lightGray
rippleTouchController.addRipple(to: levelsBtn)
rippleTouchController2.addRipple(to: topicsBtn)
rippleTouchController3.addRipple(to: settingsBtn)
rippleTouchController4.addRipple(to: instaBtn)
rippleTouchController5.addRipple(to: playBtn)
}

Unable to get the value back from a function to assign it to a label

I'm new to building an application but learning. I am trying to take value from the alert and save it to the label. This is to create a simple timetable to for the Week. I have two difficulties one is getting value back from the function to display it to my label which is placed behind the Button pressed on the Main.Sotryboard.
I am trying to get my value back here to assign to the Label MonB
#IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
//Trying to get my text input back here.
I am trying to get the value from the function below:
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
I also looked around but don't know how to save these label values so When i close the app it still remains saved as persistent.
This is the complete Code.
import UIKit
class MenuViewController: UIViewController {
//Define all the Output Labels
#IBOutlet weak var MonB: UILabel!
#IBOutlet weak var MonL: UILabel!
#IBOutlet weak var MonD: UILabel!
#IBOutlet weak var TueB: UILabel!
#IBOutlet weak var TueL: UILabel!
#IBOutlet weak var TueD: UILabel!
#IBOutlet weak var WedB: UILabel!
#IBOutlet weak var WedL: UILabel!
#IBOutlet weak var WedD: UILabel!
#IBOutlet weak var ThuB: UILabel!
#IBOutlet weak var ThuL: UILabel!
#IBOutlet weak var ThuD: UILabel!
#IBOutlet weak var FriB: UILabel!
#IBOutlet weak var FriL: UILabel!
#IBOutlet weak var FriD: UILabel!
#IBOutlet weak var SatB: UILabel!
#IBOutlet weak var SatL: UILabel!
#IBOutlet weak var SatD: UILabel!
#IBOutlet weak var SunB: UILabel!
#IBOutlet weak var SunL: UILabel!
#IBOutlet weak var SunD: UILabel!
//Define Buttons to Enable/Disable
#IBOutlet weak var MondB: UIButton!
#IBOutlet weak var MondL: UIButton!
#IBOutlet weak var MondD: UIButton!
#IBOutlet weak var TuesB: UIButton!
#IBOutlet weak var TuesL: UIButton!
#IBOutlet weak var TuesD: UIButton!
#IBOutlet weak var WedsB: UIButton!
#IBOutlet weak var WedsL: UIButton!
#IBOutlet weak var WedsD: UIButton!
#IBOutlet weak var ThurB: UIButton!
#IBOutlet weak var ThurL: UIButton!
#IBOutlet weak var ThurD: UIButton!
#IBOutlet weak var FridB: UIButton!
#IBOutlet weak var FridL: UIButton!
#IBOutlet weak var FridD: UIButton!
#IBOutlet weak var SatuB: UIButton!
#IBOutlet weak var SatuL: UIButton!
#IBOutlet weak var SatuD: UIButton!
#IBOutlet weak var SundB: UIButton!
#IBOutlet weak var SundL: UIButton!
#IBOutlet weak var SundD: UIButton!
//Define the Edit button to enable Tap Buttons
#IBOutlet var editButton: UIBarButtonItem!
//Define all the Output Buttons
#IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func MoL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func MoD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuD(_ sender: UIButton) {
ButtonTapped(sender)
}
//VIEWDIDLOAD Parameters
override func viewDidLoad() {
super.viewDidLoad()
//Hide the Navigation Bar on the screen
self.navigationController?.isNavigationBarHidden = false
//Define the Title for the page
self.title = "~~~Weekly Menu~~~"
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = self.editButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Disable UIButton by default
self.MondB.isEnabled = false
self.MondL.isEnabled = false
self.MondD.isEnabled = false
self.TuesB.isEnabled = false
self.TuesL.isEnabled = false
self.TuesD.isEnabled = false
self.WedsB.isEnabled = false
self.WedsL.isEnabled = false
self.WedsD.isEnabled = false
self.ThurB.isEnabled = false
self.ThurL.isEnabled = false
self.ThurD.isEnabled = false
self.FridB.isEnabled = false
self.FridL.isEnabled = false
self.FridD.isEnabled = false
self.SatuB.isEnabled = false
self.SatuL.isEnabled = false
self.SatuD.isEnabled = false
self.SundB.isEnabled = false
self.SundL.isEnabled = false
self.SundD.isEnabled = false
}
#IBAction func editEnable(_ sender: Any) {
//self.MonB.isHidden = false
// Alert stating Changes can be made
let alert = UIAlertController(title: "Message for Cook",
message: "Time to change the Menu
for Next Week",
preferredStyle:
UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style:
UIAlertAction.Style.default))
present(alert, animated: true, completion: {
//Enable Button capabilities
self.MondB.isEnabled = true
self.MondL.isEnabled = true
self.MondD.isEnabled = true
self.TuesB.isEnabled = true
self.TuesL.isEnabled = true
self.TuesD.isEnabled = true
self.WedsB.isEnabled = true
self.WedsL.isEnabled = true
self.WedsD.isEnabled = true
self.ThurB.isEnabled = true
self.ThurL.isEnabled = true
self.ThurD.isEnabled = true
self.FridB.isEnabled = true
self.FridL.isEnabled = true
self.FridD.isEnabled = true
self.SatuB.isEnabled = true
self.SatuL.isEnabled = true
self.SatuD.isEnabled = true
self.SundB.isEnabled = true
self.SundL.isEnabled = true
self.SundD.isEnabled = true
})
}
#objc func ButtonTapped(_ sender: UIButton)
{
// Create an alert
let alert = UIAlertController(
title: "What to cook?",
message: "Add The Name Of The Dish:",
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewToDoItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
if let Dish = alert.textFields?[0].text
{
self.FoodItemAdded(Food: Dish)
}
}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}
func FoodItemAdded(Food: String)
{
//return self.
//Trying to return the value from here.
}
//Saving the data
}
Write your function similar to this
func FoodItemAdded(Food: String)->String
{
//setting the parameter value to a variable
let string = Food
//returning new string type variable
return string
}
You can set the return value to a label like this,
self.mylabel.text = FoodItemAdded("Burger")
To store data, you can use core data/ SQLITE.
Coredata: https://youtu.be/tP4OGvIRUC4
Sqlite: https://youtu.be/c4wLS9py1rU
This is how I solved the problem:
import UIKit
import CoreData
#available(iOS 10.0, *)
class MenuViewController: UIViewController {
//Define all the Output Labels
#IBOutlet weak var MonB: UILabel!
#IBOutlet weak var MonL: UILabel!
#IBOutlet weak var MonD: UILabel!
#IBOutlet weak var TueB: UILabel!
#IBOutlet weak var TueL: UILabel!
#IBOutlet weak var TueD: UILabel!
#IBOutlet weak var WedB: UILabel!
#IBOutlet weak var WedL: UILabel!
#IBOutlet weak var WedD: UILabel!
#IBOutlet weak var ThuB: UILabel!
#IBOutlet weak var ThuL: UILabel!
#IBOutlet weak var ThuD: UILabel!
#IBOutlet weak var FriB: UILabel!
#IBOutlet weak var FriL: UILabel!
#IBOutlet weak var FriD: UILabel!
#IBOutlet weak var SatB: UILabel!
#IBOutlet weak var SatL: UILabel!
#IBOutlet weak var SatD: UILabel!
#IBOutlet weak var SunB: UILabel!
#IBOutlet weak var SunL: UILabel!
#IBOutlet weak var SunD: UILabel!
//Define Buttons to Enable/Disable
#IBOutlet weak var MondB: UIButton!
#IBOutlet weak var MondL: UIButton!
#IBOutlet weak var MondD: UIButton!
#IBOutlet weak var TuesB: UIButton!
#IBOutlet weak var TuesL: UIButton!
#IBOutlet weak var TuesD: UIButton!
#IBOutlet weak var WedsB: UIButton!
#IBOutlet weak var WedsL: UIButton!
#IBOutlet weak var WedsD: UIButton!
#IBOutlet weak var ThurB: UIButton!
#IBOutlet weak var ThurL: UIButton!
#IBOutlet weak var ThurD: UIButton!
#IBOutlet weak var FridB: UIButton!
#IBOutlet weak var FridL: UIButton!
#IBOutlet weak var FridD: UIButton!
#IBOutlet weak var SatuB: UIButton!
#IBOutlet weak var SatuL: UIButton!
#IBOutlet weak var SatuD: UIButton!
#IBOutlet weak var SundB: UIButton!
#IBOutlet weak var SundL: UIButton!
#IBOutlet weak var SundD: UIButton!
//Define the Edit button to enable Tap Buttons
#IBOutlet var editButton: UIBarButtonItem!
//Define all the Output Buttons
#IBAction func MoB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func MoL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func MoD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func TuD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func WeD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func ThD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func FrD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SaD(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuB(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuL(_ sender: UIButton) {
ButtonTapped(sender)
}
#IBAction func SuD(_ sender: UIButton) {
ButtonTapped(sender)
}
//VIEWDIDLOAD Parameters
override func viewDidLoad() {
super.viewDidLoad()
//Hide the Navigation Bar on the screen
self.navigationController?.isNavigationBarHidden = false
//Define the Title for the page
self.title = "~~~Weekly Menu~~~"
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = self.editButton
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Disable UIButton by default
self.MondB.isEnabled = false
self.MondL.isEnabled = false
self.MondD.isEnabled = false
self.TuesB.isEnabled = false
self.TuesL.isEnabled = false
self.TuesD.isEnabled = false
self.WedsB.isEnabled = false
self.WedsL.isEnabled = false
self.WedsD.isEnabled = false
self.ThurB.isEnabled = false
self.ThurL.isEnabled = false
self.ThurD.isEnabled = false
self.FridB.isEnabled = false
self.FridL.isEnabled = false
self.FridD.isEnabled = false
self.SatuB.isEnabled = false
self.SatuL.isEnabled = false
self.SatuD.isEnabled = false
self.SundB.isEnabled = false
self.SundL.isEnabled = false
self.SundD.isEnabled = false
}
#IBAction func editEnable(_ sender: Any) {
// Alert stating Changes can be made
let alert = UIAlertController(title: "Message for Cook",
message: "Time to change the Menu for Next Week",
preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default))
present(alert, animated: true, completion: {
//Enable Button capabilities
self.MondB.isEnabled = true
self.MondL.isEnabled = true
self.MondD.isEnabled = true
self.TuesB.isEnabled = true
self.TuesL.isEnabled = true
self.TuesD.isEnabled = true
self.WedsB.isEnabled = true
self.WedsL.isEnabled = true
self.WedsD.isEnabled = true
self.ThurB.isEnabled = true
self.ThurL.isEnabled = true
self.ThurD.isEnabled = true
self.FridB.isEnabled = true
self.FridL.isEnabled = true
self.FridD.isEnabled = true
self.SatuB.isEnabled = true
self.SatuL.isEnabled = true
self.SatuD.isEnabled = true
self.SundB.isEnabled = true
self.SundL.isEnabled = true
self.SundD.isEnabled = true
})
}
#objc func ButtonTapped(_ sender: UIButton)
{
// Create an alert
let alert = UIAlertController(
title: "What to cook?",
message: "Add The Name Of The Dish:",
preferredStyle: .alert)
// Add a text field to the alert for the new item's title
alert.addTextField(configurationHandler: nil)
// Add a "cancel" button to the alert. This one doesn't need a handler
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Add a "OK" button to the alert. The handler calls addNewToDoItem()
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
if let Dish = alert.textFields?[0].text {
if sender == self.MondB {
self.MonB.text = Dish
}
else if sender == self.MondL
{
self.MonL.text = Dish
}
else if sender == self.MondD
{
self.MonD.text = Dish
}
else if sender == self.TuesB
{
self.TueB.text = Dish
}
else if sender == self.TuesL
{
self.TueL.text = Dish
}
else if sender == self.TuesD
{
self.TueD.text = Dish
}
else if sender == self.WedsB
{
self.WedB.text = Dish
}
else if sender == self.WedsL
{
self.WedL.text = Dish
}
else if sender == self.WedsD
{
self.WedD.text = Dish
}
else if sender == self.ThurB
{
self.ThuB.text = Dish
}
else if sender == self.ThurL
{
self.ThuL.text = Dish
}
else if sender == self.ThurD
{
self.ThuD.text = Dish
}
else if sender == self.FridB
{
self.FriB.text = Dish
}
else if sender == self.FridL
{
self.FriL.text = Dish
}
else if sender == self.FridD
{
self.FriD.text = Dish
}
else if sender == self.SatuB
{
self.SatB.text = Dish
}
else if sender == self.SatuL
{
self.SatL.text = Dish
}
else if sender == self.SatuD
{
self.SatD.text = Dish
}
else if sender == self.SundB
{
self.SunB.text = Dish
}
else if sender == self.SundL
{
self.SunL.text = Dish
}
else if sender == self.SundD
{
self.SunD.text = Dish
}
}}))
// Present the alert to the user
self.present(alert, animated: true, completion: nil)
}

Swift: Scroll View only when a TextField or Button is hidden by the Keyboard

Below is the code that I'm trying to use to have the view scroll only when the focused textField or UIButton is hidden by the keyboard:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var buttonToBottomConstraint: NSLayoutConstraint!
#IBOutlet weak var textFieldUsername: UITextField!
#IBOutlet weak var textFieldPassword: UITextField!
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil)
}
func textFieldDidBeginEditing(textField: UITextField) {
}
func textFieldShouldReturn(textField: UITextField) -> Bool {textField.resignFirstResponder()
return true
}
func keyboardWillBeHidden(notification: NSNotification){
self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
}
func keyboardWillBeShown(notification: NSNotification){
let userInfo = notification.userInfo!
if let kbsize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size{
self.scrollView .setContentOffset(CGPointMake(0, kbsize.or), animated: true)
}
}
}
Problem: The View scrolls even when the keyboard is not hiding anything.
View at Launch:
View Upon Tapping on a TextField:
Question: How do I check to see if the focused field is hidden by the keyboard, and ONLY if it is, push it above it?
Project: TextFieldScroll
EDIT
App works fine in Portrait mode, however, not in landscape mode. Also in landscape mode, when the Username textField keyboard is up and I tap the Password textField, it remains hidden behind the keyboard. I updated the project on Github and fixed the link. Thanks
Screen shot in landscape:
var currentTextField:UITextField?
#IBOutlet weak var textFieldUsername: UITextField!
#IBOutlet weak var textFieldPassword: UITextField!
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad()
{
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil)
}
func textFieldDidBeginEditing(textField: UITextField)
{
currentTextField=textField;
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func keyboardWillBeHidden(notification: NSNotification){
currentTextField=nil;
self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
}
func keyboardWillBeShown(notification: NSNotification){
let userInfo = notification.userInfo!
if let kbsize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size
{
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: kbsize.height, right: 0)
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenHeight = screenSize.height
let height:CGFloat=screenHeight-(kbsize.height)-(currentTextField!.frame.size.width)
if(currentTextField!.frame.origin.y>=height )
{
var scrollPoint: CGPoint = CGPointMake(0.0, currentTextField!.frame.origin.y - (kbsize.height));
self.scrollView .setContentOffset(scrollPoint, animated: true)
}
}
}
func textFieldDidEndEditing(textField: UITextField) {
currentTextField=nil;
}
I don't see you setting the scrollView's contentSize. Thus, the scrollView scrolls with the keyboard since it uses the screen's size.