Bring button in cell to top windows swift4

Do

// Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200

0 in your console and run the project to try a demo. To install CocoaPods, run

// Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200

1 in your console.

Installation 📱

// Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200

2 supports Swift 5.0 since version

// Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200

3.

// Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200

2 supports Swift 4.2 since version

// Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200

5.

If you need Swift 4.0, use version 2.3.2:

  • Manually: use tag // Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200 6
  • CocoaPods: // Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200 7
  • Carthage: // Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200 8

CocoaPods

Use CocoaPods.

  1. Add // Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200 9 to your Podfile.
  2. Install the pod(s) by running dropDown.show() dropDown.hide() 0.
  3. Add dropDown.show() dropDown.hide() 1 in the .swift files where you want to use it

Carthage

Use Carthage.

  1. Create a file name dropDown.show() dropDown.hide() 2.
  2. Add the line dropDown.show() dropDown.hide() 3.
  3. Run dropDown.show() dropDown.hide() 4.
  4. Drag the built dropDown.show() dropDown.hide() 5 into your Xcode project.

Source files

A regular way to use DropDown in your project would be using Embedded Framework. There are two approaches, using source code and adding submodule.

Add source code:

  1. Download the latest code version.
  2. Unzip the download file, copy // Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200 2 folder to your project folder

Add submodule

  1. In your favorite terminal, dropDown.show() dropDown.hide() 7 into your top-level project directory, and entering the following command:

$ git submodule add [email protected]:AssistoLab/DropDown.git

After you get the source code either by adding it directly or using submodule, then do the following steps:

  • Open // Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200 2 folder, and drag dropDown.show() dropDown.hide() 9 into the file navigator of your app project, under you app project.
  • In Xcode, navigate to the target configuration window by clicking the blue project icon, and selecting the application target under the "Targets" heading in the sidebar.
  • Open "Build Phases" panel in the tab bar at the top of the window, expend the "Target Dependencies" group and add dropDown.show() dropDown.hide() 5 under DropDown icon in the popup window by clicking DropDown.startListeningToKeyboard() 1. Similarly, you can also add dropDown.show() dropDown.hide() 5 in "Embedded Binaries" under "General" tab.

Basic usage ✨

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

Optional properties:

// Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200

Display actions:

dropDown.show() dropDown.hide()

Important ⚠️

Don't forget to put:

DropDown.startListeningToKeyboard()

in your

DropDown.startListeningToKeyboard()

3's

DropDown.startListeningToKeyboard()

4 method so that the drop down will handle its display with the keyboard displayed even the first time a drop down is showed.

Advanced usage 🛠

Direction

The drop down can be shown below or above the anchor view with:

dropDown.direction = .any

With

DropDown.startListeningToKeyboard()

5 the drop down will try to displa itself below the anchor view when possible, otherwise above if there is more place than below. You can restrict the possible directions by using

DropDown.startListeningToKeyboard()

6 or

DropDown.startListeningToKeyboard()

7.

Offset

By default, the drop down will be shown onto to anchor view. It will hide it. If you need the drop down to be below your anchor view when the direction of the drop down is

DropDown.startListeningToKeyboard()

7, you can precise an offset like this:

// Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!)

If you set the drop down direction to

DropDown.startListeningToKeyboard()

5 or

DropDown.startListeningToKeyboard()

6 you can also precise the offset when the drop down will shown above like this:

// When drop down is displayed with Direction.top, it will be above the anchorView dropDown.topOffset = CGPoint(x: 0, y:-(dropDown.anchorView?.plainView.bounds.height)!)

Note the minus sign here that is use to offset to the top.

Cell configuration

Formatted text

By default, the cells in the drop down have the

dropDown.direction = .any

1 values as text. If you want a custom formatted text for the cells, you can set

dropDown.direction = .any

2 like this:

dropDown.cellConfiguration = { [unowned self] (index, item) in return "- (item) (option (index))" }

Custom cell

You can also create your own custom cell, from your .xib file. To have something like this for example:

Bring button in cell to top windows swift4

You can check out a concrete example in the Demo inside this project (go to

dropDown.direction = .any

3, line 125).

For this you have to:

  • Create a dropDown.direction = .any 4 subclass (e.g. MyCell.swift)

class MyCell: DropDownCell { @IBOutlet weak var logoImageView: UIImageView! }

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

0

  • And you're good to go! 🙆

For a complete example, don't hesitate to check the demo app and code.

Events

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

1

Dismiss modes

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

2

You have 3 dismiss mode with the

dropDown.direction = .any

5 enum:

  • dropDown.direction = .any 6: A tap oustide the drop down is needed to dismiss it (Default)
  • dropDown.direction = .any 7: No tap is needed to dismiss the drop down. As soon as the user interact with anything else than the drop down, the drop down is dismissed
  • dropDown.direction = .any 8: The drop down can only be dismissed manually (in code)

Others

You can manually (pre)select a row with:

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

3

The data source is reloaded automatically when changing the

dropDown.direction = .any

1 property. If needed, you can reload the data source manually by doing:

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

4

You can get info about the selected item at any time with this:

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

5

Customize UI 🖌

You can customize these properties of the drop down:

  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 0: the font of the text for each cells of the drop down.
  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 1: the color of the text for each cells of the drop down.
  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 2: the color of the text for selected cells of the drop down.
  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 3: the background color of the drop down.
  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 4: the background color of the selected cell in the drop down.
  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 5: the height of the drop down cells.
  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 6: the color of the background (behind the drop down, covering the entire screen).
  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 7: the corner radius of the drop down (see below if you encounter any issue)
  • // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 8: the masked corners of the dropdown. Use this along with // Top of drop down will be below the anchorView dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!) 7 to set the corner radius only on certain corners.

You can change them through each instance of

// Action triggered on selection dropDown.selectionAction = { [unowned self] (index: Int, item: String) in print("Selected item: (item) at index: (index)") } // Will set a custom width instead of the anchor view width dropDownLeft.width = 200

2 or via

// When drop down is displayed with Direction.top, it will be above the anchorView dropDown.topOffset = CGPoint(x: 0, y:-(dropDown.anchorView?.plainView.bounds.height)!)

1 like this for example:

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

6

Expert mode 🤓

when calling the

// When drop down is displayed with Direction.top, it will be above the anchorView dropDown.topOffset = CGPoint(x: 0, y:-(dropDown.anchorView?.plainView.bounds.height)!)

2 method, it returns a tuple like this:

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

7

  • // When drop down is displayed with Direction.top, it will be above the anchorView dropDown.topOffset = CGPoint(x: 0, y:-(dropDown.anchorView?.plainView.bounds.height)!) 3: Tells if there is enough height to display the drop down. If its value is // When drop down is displayed with Direction.top, it will be above the anchorView dropDown.topOffset = CGPoint(x: 0, y:-(dropDown.anchorView?.plainView.bounds.height)!) 4, the drop down is not showed.
  • // When drop down is displayed with Direction.top, it will be above the anchorView dropDown.topOffset = CGPoint(x: 0, y:-(dropDown.anchorView?.plainView.bounds.height)!) 5: If the drop down was not able to show all cells from the data source at once, // When drop down is displayed with Direction.top, it will be above the anchorView dropDown.topOffset = CGPoint(x: 0, y:-(dropDown.anchorView?.plainView.bounds.height)!) 5 will contain the height needed to display all cells at once (without having to scroll through them). This can be used in a scroll view or table view to scroll enough before showing the drop down.

Issues

If you experience the compiler error "Ambiguous use of 'cornerRadius'" on line:

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

8

Please use intead:

let dropDown = DropDown() // The view to which the drop down will appear on dropDown.anchorView = view // UIView or UIBarButtonItem // The list of items to display. Can be changed dynamically dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

9

Requirements

  • Xcode 8+
  • Swift 3.0
  • iOS 8+
  • ARC

License

This project is under MIT license. For more information, see

// When drop down is displayed with Direction.top, it will be above the anchorView dropDown.topOffset = CGPoint(x: 0, y:-(dropDown.anchorView?.plainView.bounds.height)!)

7 file.

Credits

DropDown was inspired by the Material Design version of the .

DropDown was done to integrate in a project I work on:

It will be updated when necessary and fixes will be done as soon as discovered to keep it up to date.

How do I bring the button to the front in Swift?

Swift 3. A UIButton is a UIView, so you can call -bringSubviewToFront: on your button instance. uiview 's are displayed in the order they are stacked. One can change this to say were to insert the new uiview , which in your case is a uibutton.

How to use UITableViewCell in Swift?

Create a new iOS project and give any name you wish. Make sure to select Storyboard as the interface, instead of SwiftUI..

For simplicity, leave all the generated files as they are and open Main. storyboard..

Add a button, give it a cute name, open ViewController..

How do I create a custom UITableViewCell?

Add a new file (Command + N), select Cocoa Touch Class >, then select UITableViewCell for the subclass and name the class ActorTableViewCell . Select Next > Create. We'll need to do some math to make sure we get the measurements we want.

How to set button title programmatically in Swift?

To change the text of a UIButton in Swift, you can use the setTitle() method of the button. This method takes an argument to set the button title for a particular state. Generally, we used the normal button state.