on click on label go to next page - swift

When I created a button earlier it had the ability to be an "action" IBA function..
How can I create the same thing with a label? so when I click the label my app will direct to a new page. (and how can I make it to direct to a new page? would I have to make a totally new view and in some way link it to that)
Please bare in mind, I'm a totally new to swift and have already come along way with my program,but its been pieces from several tutorials and therefor I might have a hard time understanding your answer the first time.

You can use UITapGestureRecognizer for that and below is simple example for you:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.userInteractionEnabled = true
label.text = "I'am a test label"
self.view.addSubview(label)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
label.addGestureRecognizer(gestureRecognizer)
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("next")
self.presentViewController(vc, animated: true, completion: nil)
}
}
Result:
Project sample for more Info.

Swift 3
1- Make sure you've checked the "User Interaction Enabled" in attributes inspector.
2- Code:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourVC.labelTapped))
yourLabel.addGestureRecognizer(gestureRecognizer)
3- Use the func:
func labelTapped() {
print("labelTapped tapped")
}

Related

Add Textfield MDCAlertController IOS

There is no method similar to the addtextfield method in Uialertcontroller. I haven't found any examples of how I can customize MDCAlertControllers. Anybody have an idea?
I think this is not possible. The docs say:
MDCAlertController class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
This is now possible by using accessoryView.
Simply set it to be your custom view and set the alert message to be blank.
alertController.accessoryView = myCustomView
Code example (using SnapKit for layout)
let title = "My title"
let alertController = MDCAlertController(title: title, message: "")
let confirmAction = MDCAlertAction(title:"Confirm") { [weak self] _ in
//your action here
}
let cancelAction = MDCAlertAction(title:"Cancel")
let width = UIScreen.main.bounds.width * 0.91
let testView = UIView()
let button = UIButton()
button.setTitle("Test", for: .normal)
testView.addSubview(button)
button.snp.makeConstraints { (make) in
make.center.equalToSuperview()
}
testView.backgroundColor = .blue //just to show where the view is
testView.snp.makeConstraints { (make) in
make.width.equalTo(width)
make.height.equalTo(100)
}
alertController.accessoryView = testView
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
alertController.defaultTheming()
present(alertController, animated:true)
Result from above code

How To Shape Orientation of Ipad-like Popover?

I have 2 questions.
1) Currently, my iPad popover is working but the arrow is pointing downward on top of button. How do I flip it so the arrow points upward and the ViewController is under the button?
2) how do I keep the layout the same if I turn my phone (it will become full screen).
Below is my code for the popover that has emojis.
#IBAction func emojiButtonTapped(_ sender: UIButton) {
let VC = storyboard?.instantiateViewController(withIdentifier: "EmojiController") as! EmojiViewController
VC.preferredContentSize = CGSize(width: 200, height: 100)
let navController = UINavigationController(rootViewController: VC)
navController.modalPresentationStyle = UIModalPresentationStyle.popover
let popOver = navController.popoverPresentationController
popOver?.sourceView = sender
popOver?.sourceRect = sender.bounds
popOver?.delegate = self
self.present(navController, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
To answer the first question: You can use the permittedArrowDirections property of the UIPopoverPresentationController to give a hint which arrows you allow. In your case, use UIPopoverArrowDirection.up
The second question I do not quite understand - what do you mean with
how do I keep the layout the same if I turn my phone
What layout?

Swift 3 subclassing uiviewcontroller created programmatically

I have a slide in menu that I use to programmatically create a UIViewController:
func showControllerForMenu(_ menus: Menu) {
let menuChoiceViewController = UIViewController()
menuChoiceViewController.view.backgroundColor = UIColor.white
menuChoiceViewController.navigationItem.title = menus.name
navigationController?.navigationBar.tintColor = UIColor.black
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.black]
navigationController?.pushViewController(menuChoiceViewController, animated: true)
}
This works fine, what I'm trying to figure out is how to subclass the UIViewController in order to add controls and functionality. Any help would be appreciated.

How to Change back button title on navigation controller in swift3?

I want to change the title of backbutton to any name in swift 3.I tried many ways but none of them worked for me.
self.navigationItem.backBarButtonItem?.title="Title"
self.navigationController?.navigationItem.backBarButtonItem?.title="Title"
Just for information i have written below code in appdelegate.
let backImage : UIImage = UIImage(named:"backArrow")!
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, 0), for: .default)
IQKeyboardManager.sharedManager().enable = true
self.window?.backgroundColor = UIColor.white
Navigation item back button name will be same as the title of previous view controller which is pushing it to the navigation stack :)
So if VC A pushes VC B, back button in VC B will be A.
So all you can do is, to change the title of the previous viewController before pushing the new viewController using code :)
self.navigationItem.title = "ABCD"
And in ViewWillAppear of VC A,you can revert the title back to whatever it was earlier :)
self.navigationItem.title = "Back to xyz"
All that being said, if you don't want all this circus :) you can simply hide the default back button using,
self.navigationItem.hidesBackButton = true
in your VC B, create a UIBarButton item, set whatever the title you want to set and then set that as leftBarButtonItem :) using,
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("ABCD", comment: "ABCD"), style: .plain, target: self, action:#selector(self.abcdTapped:)
of course now that will not show "<" icon :) Now if you want that as well you can add it as a image to back bar button item :) but its cumbersome :)
Hope it helps :)
You will have to set the backBarButtonItem property of the navigationItem of the viewController that you push the said viewController from.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
However, you must set this for each viewController.
This is the way:
extension UINavigationController {
func addCustomBackButton(title: String = "Back") {
let backButton = UIBarButtonItem()
backButton.title = title
navigationBar.topItem?.backBarButtonItem = backButton
}
}
In Swift 3.0 put below code in appdelegate didFinishLaunchingWithOptions method its worked perfectly for me
let backImage = UIImage(named: "BackNavigation")?.withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), for: .default)
The last line will remove the title of Navigation Back Button if you don't want to remove title then just comment it
I didn't find the answer that I was looking for so I share with you my solution.
Sometimes you have to change the text of the back button in the parent ViewController and not in the ViewController where seems to be defined the back button, remember that a navigation controller stacks ViewControllers one after another.
In my case I did this on the function prepare(for segue: ) of the "ParentViewController":
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showChildViewController" {
if let childViewController = segue.destination as? ChildViewController {
let backItem = UIBarButtonItem()
backItem.title = "Back"
navigationItem.backBarButtonItem = backItem
}
}
Try following steps to set image to your back button..
Output:
Step 1:
Add following code to your AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
var backButtonImage = UIImage(named: "Your Image Name")
backButtonImage = backButtonImage?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 0)
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
return true
}
Step 2:
Add following code to your MainVC
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
title = "Title 1"
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 20)!]
}
Step 3:
Add following code to your DestVC or 2ndVC
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
title = "Title 2"
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 20)!]
}
Step 4:
Select your navigationBar from your StoryBoard and goto Attribute Inspector. Under Navigation Item change your Back Button name enter a empty space or programatically create a back button with plain title..
Step 5:
Add icon image to your Assets. 1x29pt,2x58pt and 3x87pt. I am not sure about the asset image size.Check with apple doc about the size class..
Update:
My Similar answer related to this post.
How to customize the navigation back symbol and navigation back text?
You can easily do that from the storyboard by setting the title of the previous screen. Image explaining how to do that from storyboard - or you can do that by adding the following code to the view controller you're navigating BACK to.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
let backItem = UIBarButtonItem()
backItem.title = "Title"
navigationItem.backBarButtonItem = backItem
}
in viewDidLoad()
let backBarButtonItem = UIBarButtonItem(title: "You back button title here", style: .plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backBarButtonItem

Why is my image not showing up Fullscreen?

I have the following problem. I have about 10 different images which are in a view controller. Now I want the image to switch to fullscreen when they are tapped on. I already set the allow user interaction option to yes, and put in the following code i found on this website:
import UIKit
class ImageViewController: UIViewController {
#IBAction func imageTapped(sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .blackColor()
newImageView.contentMode = .ScaleAspectFit
newImageView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}
func dismissFullscreenImage(sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
Does anyone know why my images are not tappable and turning fullscreen?
Make sure both the image view have userInteractionEnabled = YES and
here's an example for what you are trying to do here https://github.com/abhinavsingh77/ImagePreview