HomeОбразованиеRelated VideosMore From: Jared Davidson

Let's make a Drop Down Menu! (Swift 4 in Xcode 9)

507 ratings | 39976 views
DevMountain (Sponsor) - https://goo.gl/kgkx3r Today we dive deep into making our very own drop down menus from scratch, using Swift 4 in Xcode! Enjoy! :D Project Files - https://github.com/Archetapp/Drop-Down-Menu Song - Vexento | Potato Cream ~Social Stuff~ Website - http://www.archetapp.com Twitter - http://www.twitter.com/archetapp
Html code for embedding videos on your blog
Text Comments (127)
Jared Davidson (1 year ago)
The video starts at 1:06. Enjoy! GitHub is linked in description. :] Feel free to share! 🙌
Mayanka Jha (3 days ago)
Hey Jared, how can we custom change the location of our button in case we don't want it in the center.
MD RASHID IQUBAL (7 months ago)
you are doing good. Great!. I really enjoy your tutorials...Thanks!
Jose Varghese (1 year ago)
Hey!!! Jared i like your videos. i have a question, is it possible to write a code in iOS to read the amount of RAM used collectively by all apps ??
Mehmet Filiz (19 days ago)
You're delivery of your self made great content is razztwizzling.
WingedDestinyX (1 month ago)
Nice tutorial! Great job! Minor remark: you can drop the `self.` before variables when it is not set with a variable with the same name or not in a closure ;) But I really like this tutorial :D
David Shi (1 month ago)
Thank you Jared for the awesome tutorial. You've been a huge help to me self learning iOS development. I have a quick question I hope someone could help clarify for me... Why is the code in line 19 needed? button = dropDownBtn.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) Doesn't line 13 already create and initialize the object dropDownBtn? var button = dropDownBtn()
Heriberto Rodriguez (2 months ago)
WARNING: I think this code will cause the app to crash if this view controller is pushed onto a navigation controller and then popped. The reason is that didMoveToSuperview is called whenever the superview of the button changes, which in this case is the view of the view controller. This view will be nil when the this view controller is popped and the didMoveToSuperview method of your button will be called. This will try to set the constraints of dropView to the bottom anchor of the button, but they don't share a superview anymore. Just check if superview is nil before setting constraints.
Sergio (2 months ago)
Hi Jared, good job, I can ask you how I can, with your system, pass the string on a label instead of inserting it in the title of the button? Thank you very much
San Vlogs (2 months ago)
how do i put it in the corner
Elliot Williams (2 months ago)
You are what inspired me to make apps and publish them on the Apple App Store under Elliot Williams. I’d love to see you try and recreate my Egg Game in a video in the future it’s already becoming popular a bit my Egg Game :) thanks so much for all of your videos
Harish Kumar (2 months ago)
Great video Jared. This was just what i needed. I have one problem - I have this drop down button in a view that was pushed by a nav controller. When I hit the back button to go to the previous page I hit the same error you encountered with the constraints. It says it encountered the error during auto release pool drain. Can you please let me know what I need to do to get rid of this error?
sean campbell (3 months ago)
Hi is there any way to send out the color you picked to someone’s email? Like a submit button after at the bottom?
손기석 (3 months ago)
super nice & super awsome
Riley Lunz (3 months ago)
How do You customise and change the buttons position on the view controller @Jared Davidson
Riley Lunz (3 months ago)
how would I input data from a differnt table view into these cells??
Quinntin Duong (3 months ago)
Is there anyway to make another drop down list from this code? Could anyone help me?
SubzeroWins (3 months ago)
Genius!
Christian Milholland (3 months ago)
Hey Jared I was wondering if you could make a video or tell me how to make the options in the drop down menu actions
Edwin Alvarado (4 months ago)
Thats great! Cool
cabyambo (4 months ago)
Why can we only change the width of the button using "button.widthAnchor..." and not in the initialization of the button?
Ali Mizan (5 months ago)
Thank you for the excellent tutorial.
Eli Tzar (5 months ago)
Hey guys, my touches for the tableview seem to be going straight through the dropDownView and interacting with my UI elements behind it, any idea what to do? I cannot select anything
alex weimers (6 months ago)
Is it possible to make a dropdown menu in an alert popup ?
Niels Krieg (3 months ago)
No.
Great video!!!!
Selene Yachts Official (7 months ago)
Hi, i've got a problem, when the option is selected in the dropdown menu, it change the button label, that's perfect, but i need also to call an action in my main controller, how can we do that ? Thanks
Marl Atkins (7 months ago)
I noticed that you didn't use: dequeueResuableCell . Why is that not a concern?
Marl Atkins (7 months ago)
You're WAY too young to be teaching me! lol Great Job! Concise & complete, with code.
David G (6 months ago)
Never too young to teach & Never too old to learn!
Mosa Abu Zeid (8 months ago)
your owsoame
alfee duggie (9 months ago)
how do i get my screen like that!!! WAAAH
Ivan Camilo Damian (9 months ago)
good video, the dropdown is working correctly, but I am not able to make pressing one of the items an action in the view, specifically change the value of a label, can someone help me?
Sergio (2 months ago)
Some problem
önder güler (4 months ago)
i have the same problem
Yasmeen Hesham (9 months ago)
i created this drop down list as you created , but i need to send value from dropDownView to the contained ControllerView
michael vivar (6 months ago)
create another protocol to cascade the value to your vc
Fabien Maurice (7 months ago)
Same problem, did you find a solution ?
Abhishek Banerji (9 months ago)
i have an issue. im trying to use this in a multi screen app (in a navigation controller). this drop down button will be in a screen accessed through a main screen. the button itself works fine. BUT.....when i click the top left button to go back to the main screen the app crashes and throws some constraint errors this is the console output of the app: 2018-06-20 15:50:14.691943+0530 dropdownmenu[3109:297014] [Accessibility] ****************** Loading GAX Client Bundle **************** 2018-06-20 15:50:25.849883+0530 dropdownmenu[3109:297014] [general] Caught exception during autorelease pool drain NSGenericException: Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x1c0276fc0 "dropdownmenu.dropDownView:0x101c20e10.top"> and <NSLayoutYAxisAnchor:0x1c0276d00 "dropdownmenu.dropDownBtn:0x101c1ef10'Colors'.bottom"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal. userInfo: (null) 2018-06-20 15:50:25.850523+0530 dropdownmenu[3109:297014] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x1c0276fc0 "dropdownmenu.dropDownView:0x101c20e10.top"> and <NSLayoutYAxisAnchor:0x1c0276d00 "dropdownmenu.dropDownBtn:0x101c1ef10'Colors'.bottom"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.' *** First throw call stack: (0x1816cad8c 0x1808845ec 0x1816cac6c 0x182016c5c 0x100d39adc 0x100d39dc8 0x18b59a460 0x18b2afc24 0x18b70eeb0 0x18b2edddc 0x18b2aa548 0x1808a213c 0x181590aac 0x181670bd0 0x181590da8 0x183575020 0x18b5ad758 0x100d3f9d8 0x181021fc0) libc++abi.dylib: terminating with uncaught exception of type NSException
Sadie Flick (10 months ago)
Question: The drop down menu works for me, but when I leave the page I get a SIGABRT error: Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x60000046e840 "iCount.dropDownView:0x7fbc49f0c390.top"> and <NSLayoutYAxisAnchor:0x604000479f40 "iCount.dropDownButton:0x7fbc49c115f0'day'.bottom"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
CD (10 months ago)
same. please let us know if you find a fix first!
The Crazy Collab Bros (11 months ago)
Guys I have a question, How would I make it so the option the user selected is saved to a global variable? Please I need help and would really love your help. I need this. Thank you @jared
CD (10 months ago)
https://www.youtube.com/watch?v=DBWu6TnhLeY
The Crazy Collab Bros (10 months ago)
@xTougeluv
The Crazy Collab Bros (10 months ago)
Thank you so much, i dont know how but i will look into it.
CD (10 months ago)
by using protocols. add a protocol to the dropDownBtn class
KEV (11 months ago)
Hey Jared I am using this drop down menu to replace my old drop down menu. However, there is a sigabrt that our team cannot fix. In our information page, when we enter the page through a table view, use it and go back out, it crashes.
dan brown (4 months ago)
+KEV I ran into the same issue, if someone is reading this and having the same issue add 'self.addSubview(dropView)' to the top of the didMovetoSuperview() function
KEV (10 months ago)
This is not really the problem. I have already fixed it. Still , thanks for your help. The real problem was a constraint problem that caused a sigabrt.
CD (10 months ago)
see my answer above. be sure to set all delegates to nil before you back out. do self.<your button outlet name>.dropView.delegate = nil self.<your Button outlet name>.menuToggleDelegate = nil wherever you trigger the segue to go back. If you need more help let me know!
Dary Satyanegara (1 year ago)
You got commentated by lets build that app
KEV (1 year ago)
Thanks for the tutorial Jared! I modified it for my school's app using yp.py's custom table view cell design. We used to use a stack view for it (Stack View Magic) but it was not as good as this design!
Zain Anjum (1 year ago)
beautiful video love that dropdown specially favourite because no storyboard <3
Eris Feng (1 year ago)
Thanks!
anthonyciro88 (1 year ago)
hey Jared, New to Swift and I was wondering how to set a label's text to the selected option in the dropdown menu?? hope that makes sense!!
CD (10 months ago)
look into making protocols. you would have to write a custom protocol to capture this
Kevin Shaw (1 year ago)
I just liked and SUBSCRIBED to you, can you please just answer ONE THING for me... can you . tell me how to change where the text box is on the X and Y axis'??? I can't figure it out, but your method is brilliant
Gustavo Severo (1 year ago)
Hi Jared I saw Mark Moeykens video https://www.youtube.com/watch?v=K89-RLzYxQM&t=693s about how to do a side menu and it works fine, but I need the side menu has a scroll to roll up and down. I ask it for him, so he told me about you. Do you have a scroll side menu video? Since now, thank you for your attention.
Konrad Bald (1 year ago)
Everything worked perfectly! Thank you for an awesome video!
Jared Davidson (1 year ago)
You're sending me very mixed messages, haha. I'm glad in the end it worked out for you though. :)
Matthew Perrault (1 year ago)
Found this to be VERY frustrating! I've been trying to figure this out and look for examples for well over 2 hours now. you're the 4th video I've looked at and are no better than any of the others. you call it a tutorial but start at step 857. Good tutorials start at step 1 and go step by step. They don't jump into the middle of things. Thumbs down for a useless video.
Konrad Bald (1 year ago)
The source code is included. Why don't you build it from there and problem solve. Or go take that boot camp course at the start of the video lol
Jared Davidson (1 year ago)
What are you talking about? I literally show from building the project to the end, what more do you want?
Thank you, Jared!)
Julian Albulescu (1 year ago)
I have a question, how do I put a image in the dropdown menu?
Tirta Aditya (5 months ago)
just add image to dropdown subview and make constraint of it
Julian Albulescu (1 year ago)
Hi Jared, I love your videos but I have one question for this one. How do I make a button dropdown, that once a player clicks on that, it brings them to a View Controller that I have made in my storyboard. Because that is what I need, a button that can bring the play to a view controller that I have created in the storyboard. Your help is greatly appreciated. Thanks, Julian
Omkar Sawant (11 months ago)
did you get any solution for this
Alex Hadfield (1 year ago)
Your tutorials are amazing! Keep it up!
waleed matar (1 year ago)
I.m sorry 😄😃
waleed matar (1 year ago)
its very bad error error error error error
The_Miner27 (1 year ago)
Where did you learn all this?
Sigor Ezz (1 year ago)
21:19 What exactly does this do: self.dropView.center.y += self.dropView.frame.height / 2? You set the height to 150 and then you change the center to self.dropView.frame.height / 2? I don't get it.
Other Stuff (1 year ago)
Thumbs up for No Storyboard!
Михаил Сеин (1 year ago)
Hello, Jared! thanks for the training of creating a drop-down-button, but the application is crashed, can you look my issue? https://github.com/Archetapp/Drop-Down-Menu/issues/3
David Reed (1 year ago)
code snippet..... override func viewWillLayoutSubviews() { button.setUpConstraints() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } protocol dropDownProtocol { func dropDownPressed(string : String) } class dropDownBtn: UIButton, dropDownProtocol { func dropDownPressed(string: String) { self.setTitle(string, for: .normal) self.dismissDropDown() } var dropView = dropDownView() var height = NSLayoutConstraint() override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = UIColor.darkGray dropView = dropDownView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0)) dropView.delegate = self dropView.translatesAutoresizingMaskIntoConstraints = false } override func didMoveToSuperview() { self.superview?.addSubview(dropView) self.superview?.bringSubview(toFront: dropView) height = dropView.heightAnchor.constraint(equalToConstant: 0) } func setUpConstraints() { dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true }
David Reed (1 year ago)
to fix this you need to setup the dropdown constraints during the viewWillLayoutSubviews()
David Reed (1 year ago)
yes this is a bug with Jared's approach. If you implement his approach on a view that gets dismissed you get this error about the constraints not having a common ancestor. Unfortunately I don't have a solution to fix this but maybe Jared will weigh in??
Jeyhun Danyaloff (1 year ago)
you can just init button with DropDownButton(type: .custom) and this constructor calls the init(frame: CGRect) as well
Bahe Hachem (1 year ago)
Hey Jared, Glad to see you're starting to embark on the "StoryBoard-free train". Loving it
farshid shokrollahi (1 year ago)
hi jared , could you please make a tutorial to use Adcolony in our swift ios project , there isn't any tutorial for this, and the documentation of adcolony is not clear . thank you
Neill Barnard (1 year ago)
Hi, i am new to swift, busy designing a simple game, but before i get to deep into it, will all my sprites/images resize automatically, for any device i use ? Or what should i do ? Btw i think you should make a video, to convert the ios app to android
Yury Bogdanov (1 year ago)
Hey there :) It might be a good idea to make improved version of this tutorial with some kind of Data Source (for providing list of menu items) and/or Delegate (for providing actions for selected items) for this drop down menu. Anyway, this one can give a good start for people who wants to implement their own dropdown menu solutions :) good job!
Dan K. (1 year ago)
“Boobcamp” heheh
Zach Delson (1 year ago)
Hey jared could you check out my game? https://itunes.apple.com/us/app/speed-touch/id1270867219?ls=1&mt=8 I used many of your videos to help me build this thanks!!
Zain Anjum (1 year ago)
that's great that you don't use storyboard anymore (y)
Shender Ramos (1 year ago)
Awsome video as always ... what camera are you using? Im thinking of getting a nice camera to record my videos 👍🏽 keep the good work Jared
KidFaded (1 year ago)
Can you make another Snapchat Like menu Swift 4 Xcode 9? and pls make a login and sign up tutorial ?
Francesco Laiti (1 year ago)
You're amazing! Can you make a video on how to implement 3D Touch for quick look and home app shortcuts? Thanks Jared ✌️
Mark Moeykens (1 year ago)
Thanks, Jared. Nice video. Man, that .translatesAutoResizingMaskIntoConstraints gets me ALL the time at work. "Why am I not seeing this control?" It's usually always because I haven't set this to false. Just to let others know what this property does: If you select a view on your storyboard and go to the Size inspector, you see the Auto Resizing box with the red arrows. Those are the "Auto Resizing Masks". iOS takes those arrows and translates them into constraints for you. And this is what Jared didn't want because he is setting his own sizes. If he didn't turn this off then his views will render with zero height and zero width.
7NEMO (1 month ago)
After 3 hours headache you have just redeemed me :) Thank you very much Mark!
Marl Atkins (7 months ago)
That's a great compliment, coming from you. Your work is excellent.
Mark Moeykens (1 year ago)
No problem, Sigor!
Sigor Ezz (1 year ago)
That explanation was helpful! Now I know why you always set that long property to false. Thank you!
Jason Wilkins (1 year ago)
How hard would it be to convert this over to storyboard? I tried to add a button to a storyboard, and make it of class DropDownButton, and then only set the title, and drop down options in the view controllers viewDidLoad, but that crashed at-> required init aDecoder. Anyone know the solution to tweaking this for storyboards? Thanks
Stewart Lynch (1 year ago)
I am working on making this a Designable Storyboard class so I will post when done. In the meantime, if you want to use it in the storyboard, you have to modify the required init for the DropDownBtn Class to include the setup options. The best way to do this is to extract the setup stuff from the override init into its own function. I call it setup(). Then you can just call setup() after super.init in both the override init and the required init.
Musaab Qamar (1 year ago)
Jared I want to discuss one of my project with you. . . Please provide me your contact detail. . . i need your supervision. .
Julian Arias Maetschl (1 year ago)
Congratz for subs! Great channel :)
Kai Stasch (1 year ago)
Hey Jared, thanks a lot. And no storyboard? Great !!!!
Joshua Murray (1 year ago)
Great tutorial thank you Jared I have been having some trouble with my tableview and I'm pretty sure this answered my questions! Thanks for being awesome
Nicholas Cameron (1 year ago)
Why not just use a stack view and hide/show the views with an animation around it? any reason in particular?
Nicholas Cameron (1 year ago)
Yes true that! I half commented to see who's thinking out here! thanks Mark
Mark Moeykens (1 year ago)
Hey Nicholas, that's definitely an option but one of the biggest advantages of a table view is that it has scrolling built in. He also gets the row selection too that he can respond to and set a value. That's my guess anyway. :)
Arturo Basilis (1 year ago)
Yes please do it with storyboards,, for begginers is best. I really like using story boards, otherwise this channel will be watched for medium, probably advanced devs. But if you want to continue this path, sadly I will have to go to another channel.
Otto Linden / RoL (2 months ago)
I thought so as well when I started but now ≈ 2 months later (still learning) I know that you need to know both ways to add things to the screen! (via storyboard and code!)
Han Ibrahim (1 year ago)
can you go back to using the story board
黄成 (1 year ago)
How to put it in the navigation ?Also once click one button like "Green ", how to change a VC?(New learner 👀)
Ricardo Martins (1 year ago)
Very good! Thanks man
Jared Davidson (1 year ago)
Ricardo Martins Glad you like it Ricardo! :D
Lets Build That App (1 year ago)
Cool, you probably want to follow swift conventions and have a capital letter to start your class.
parivesh sharma (1 year ago)
although sometime I get help from you, but I feel like you know less on what you are explaining.
Ksk Royal (1 year ago)
Line 35 Correction : dropDownBtn -> DropDownBtn . You Are 99.9 % Perfect.
Aaron Renfroe (1 year ago)
Lets Build That App I almost couldn’t handle it.
Jared Davidson (1 year ago)
Lets Build That App yeah, I noticed what I was doing when I was editing... Too late. :/ in the future for sure though!
Vitor Ferraz (1 year ago)
I really liked your implementation , its one of the best that i have ever seen. Really easy to do
Jared Davidson (1 year ago)
+Vitor Ferraz Thanks man! :D
Celil Bozkurt (1 year ago)
No storyboards! You are a hero :)
Caleb Dubow (1 month ago)
^^^ Thank you so much!
Jared Davidson (1 year ago)
+Celil Bozkurt 🙌
Johan Albrectsen (1 year ago)
Yes, finally ditching the storyboards. I'm very proud of you.
Jared Davidson (1 year ago)
+Johan Albrectsen Haha, the switch has been pretty rough. But it's been worth it. :]
Rohit Navalgund (1 year ago)
It’s Xcode9. :) (title.) Edit: thanks for being awesome and making tutorials available for everyone ❤️.
Jared Davidson (1 year ago)
+Rohit Navalgund Oh whoops! Haha, fixing now. Glad you like the video! :D
Vandan Patel (1 year ago)
Thanks a lot for your work, Jared! I will definitely try this as soon as I can.
Jared Davidson (1 year ago)
Vandan Patel Awesome! Hope you like it! :D
Porter Jones (1 year ago)
First :)
Jared Davidson (1 year ago)
Porter Jones 🙌🙌🙌

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.