Making a drop down list using swift?
A 'drop down menu' is a web control / term. In iOS we don't have these. You might be better looking at UIPopoverController
. Check out this tutorial for a bit of an insight to PopoverControllers
http://www.raywenderlich.com/29472/ipad-for-iphone-developers-101-in-ios-6-uipopovercontroller-tutorial
How to create dropdown in Swift UI?
You can use the combination of GroupBox and DisclosureGroup to achieve what you're looking for, you can modify below code according to your requirements
import SwiftUI
struct ContentView: View {
//MARK: - PROPERTIES
let nutrients: [String] = ["Energy", "Sugar", "Fat", "Protein", "Vitamins", "Minerals"]
//MARK: - BODY
var body: some View{
GroupBox(){
DisclosureGroup("Nutritional value per 100g") {
ForEach(0..<nutrients.count, id: \.self){ index in
Divider()
.padding(.vertical, 2)
HStack{
Group{
Image(systemName: "info.circle")
Text(nutrients[index])
}//: GROUP
.foregroundColor(.gray)
.font(.system(.body).bold())
Spacer(minLength: 25)
Text("1g")
.multilineTextAlignment(.trailing)
}//: HSTACK
}//: LOOP
}//: DISCLOSURE
}//: BOX
}
}
//MARK: - PREVIEW
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You can customise Toggle with .toggleStyle modifier, first you have to create your style like this
struct CheckboxStyle: ToggleStyle {
func makeBody(configuration: Configuration) -> some View {
return HStack{
//IMAGE
Image(systemName: configuration.isOn ? "checkmark.square" : "square")
.font(.system(size: 30, weight: .semibold, design: .rounded))
.onTapGesture {
configuration.isOn.toggle()
}
//LABEL
configuration.label
}//: HSTACK
}
}
and then apply it on your Toggle like this
Toggle("Placeholder label", isOn: .constant(true))
.toggleStyle(CheckboxStyle())
How to create a drop down menu
It's alertController with actionSheet. User this method in your button action for example.
func handleAlert()
let alertController = UIAlertController(title: "Change Profile Photo", message: nil, preferredStyle: .actionSheet)
//First action
alertController.addAction(UIAlertAction(title: "Remove current photo", style: .destructive, handler: { (_) in
// Your remove photo code here
}))
alertController.addAction(UIAlertAction(title: "Import From Facebook", style: .default, handler: { (_) in
// Your import from facebook code here
}))
//Cancel action
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}
Dropdown list in swift
There are a number ways to do it, my suggestions would be something similar to as follows:
When you initialise the view controller, your dropdown view is offset and hidden behind the navigation bar. Do this either with Layout Constraints or using the view's frame, depending on your preferred set up.
var isAnimating: Bool = false
var dropDownViewIsDisplayed: Bool = false
func viewDidLoad() {
super.viewDidLoad()
let height: CGFloat = self.dropDownView.frame.size.height
let width: CGFloat = self.dropDownView.frame.size.width
self.dropDownView.frame = CGRectMake(0, -height, width, height)
self.dropDownViewIsDisplayed = false
}
Then link up an action to the BarButtonItem that, when pressed, displays the view if hidden or hides if visible using an animation.
@IBAction func barButtonItemPressed(sender: UIBarButtonItem?) {
if (self.dropDownViewIsDisplayed) {
self.hideDropDownView()
} else {
self.showDropDownView()
}
}
func hideDropDownView() {
var frame: CGRect = self.dropDownView.frame
frame.origin.y = -frame.size.height
self.animateDropDownToFrame(frame) {
self.dropDownViewIsDisplayed = false
}
}
func showDropDownView() {
CGRect frame = self.dropDownView.frame
frame.origin.y = self.navigationBar.frame.size.height
self.animateDropDownToFrame(frame) {
self.dropDownViewIsDisplayed = true
}
}
func animateDropDownToFrame(frame: CGRect, completion:() -> Void) {
if (!self.animating) {
self.animating = true
UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: { () -> Void in
self.dropDownView.frame = frame
}, completion: (completed: Bool) -> Void in {
self.animating = false
if (completed) {
completion()
}
})
}
}
All that is left for you is to define your dropDownView and link it up correctly.
I hope that helps, please comment if there is anything you don't understand
Related Topics
Uicollectionview Horizontal Paging Not Centered
Detecting If a User Is Moving in a Car
How to Get Screen Size Using Code on iOS
Implementing Unit Testing with iOS
Uitextview Content Size Different in iOS7
Uicollectionview Select and Deselect Issue
How to Get Current Longitude and Latitude Using Cllocationmanager-Swift
Uicollectionview Sticky Header in Swift
iOS Swift Mapkit Custom Annotation
How to Get Selected Value from Uipickerview
What Do Horizontalaccuracy and Verticalaccuracy of a Cllocation Refer To
Can't Install Watchkit App on Apple Watch
Non Power of Two Textures in iOS
How to Use Crashlytics with iOS/Os X Today View Extensions
Url Scheme "Open Settings" iOS
"Interfaceorientation" Is Deprecated in iOS 8, How to Change This Method Objective C