iOS homemade controls similar to drop-down boxes

Source: Internet
Author: User

Recently done in the logistics of the app, the number of units of goods to be selected in a given range (such as: Ton, Fang, pieces), but iOS does not resemble Android in the dropdown box, I think of the way to self-made one, the effect is satisfactory; The project team of the undergraduate will help me do iOS development, Thank you here!!!

The first step: New Project Xialakuang (the name is more vulgar, the new method is no longer the same ...) )

Step Two: Operation Main.storyboard

--"Drag a UIButton (named title" Ton ", background for" light gray ", alignment in the horizonal choose left), and then drag a uiimage image as a triangular arrow picture, Make the button look like a drop-down box;

Note: In the future, clicking this button will bring up the selector, and after selecting the content, it will change the title of the button to the content of our selection;

---"Drag a toolbar, Pickerview control to the bottom of the button (a little bit smaller, after all, we choose a few things), and then drag an item control to toolbar to" done ", or you can drag a flexible to the left of item. Space makes the "Finish" button on the right, and then toolbar and Pickerview's hidden tick;

After the completion of the following:

Step three: wiring (between control and VIEWCONTROLLER.M)

--"UIButton connect two lines, a outlet" button ", an action" btnaction ";

-"Toolbar a outlet" Toolbar ";

-"Uipickerview a outlet" picker ";

--"Complete" button on toolbar with an action "selectedaction";

---"to Pickerview connect DataSource and Delegate;(method and the same way as UITableView to display list data, see my previous blog)

Fourth step: Encode

#import "ViewController.h"@interfaceViewcontroller () <UIPickerViewDelegate,UIPickerViewDataSource>@property (Weak, nonatomic) Iboutlet UIButton*danweibtn;-(Ibaction) Btnaction: (IDsender; @property (weak, nonatomic) Iboutlet Uitoolbar*Toolbar, @property (weak, nonatomic) Iboutlet Uipickerview*Picker;-(Ibaction) Selectedaction: (ID) sender; @property (nonatomic, strong) Nsarray*Danwei;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib._danwei = [Nsarray arraywithobjects:@"tons",@"Square",@"pieces", nil];}-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{return 1;}-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{return_danwei.count;}-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{return[_danwei Objectatindex:row];}- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component{[self.danweibtn SetTitle:[_danwei Objectatindex:row] forstate:uicontrolstatenormal];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(Ibaction) Btnaction: (IDSender {self. Picker.hidden=NO; Self. Toolbar.hidden=NO;}-(Ibaction) Selectedaction: (IDSender {self. Picker.hidden=YES; Self. Toolbar.hidden=YES;}@end

Eventually:

Did not do that black triangular pattern rotation, usually if you click the drop-down box after the triangle will rotate; interested friends can try

    Triangle picture. Transform = Cgaffinetransformmakerotation (M_PI);

iOS homemade controls similar to drop-down boxes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.