今天给大家列举出来UI中的一些小控件的用法。方便大的学习与使用

一些方法和属性我们能够查看API文档。不必将每一个控件的功能都记住,

由于在使用的过程中,我们能够查看API文档。方便使用,我们仅仅要记住常见的一些方法

这里列举几个样例,其它的还要靠自己在以下学习

1.分段控制器

//
// UISegmentedControl.h
// UIKit
//
// Copyright (c) 2005-2014 Apple Inc. All rights reserved.
// #import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIControl.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UIGeometry.h>
#import <UIKit/UIBarButtonItem.h> typedef NS_ENUM(NSInteger, UISegmentedControlStyle) {
UISegmentedControlStylePlain, // large plain
UISegmentedControlStyleBordered, // large bordered
UISegmentedControlStyleBar, // small button/nav bar style. tintable
UISegmentedControlStyleBezeled, // DEPRECATED. Do not use this style.
} NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect"); enum {
UISegmentedControlNoSegment = -1 // segment index for no selected segment
}; typedef NS_ENUM(NSInteger, UISegmentedControlSegment) {
UISegmentedControlSegmentAny = 0,
UISegmentedControlSegmentLeft = 1, // The capped, leftmost segment. Only applies when numSegments > 1.
UISegmentedControlSegmentCenter = 2, // Any segment between the left and rightmost segments. Only applies when numSegments > 2.
UISegmentedControlSegmentRight = 3, // The capped,rightmost segment. Only applies when numSegments > 1.
UISegmentedControlSegmentAlone = 4, // The standalone segment, capped on both ends. Only applies when numSegments = 1.
}; @class UIImage, UIColor; NS_CLASS_AVAILABLE_IOS(2_0) @interface UISegmentedControl : UIControl <NSCoding>
{
@private
// Note: all instance variables will become private in the future. Do not access directly.
NSMutableArray *_segments;
NSInteger _selectedSegment;
NSInteger _highlightedSegment;
UIView* _removedSegment;
UIBarStyle _barStyle;
id _appearanceStorage;
UIView *_backgroundBarView;
CGFloat _enabledAlpha;
struct {
unsigned int style:3;
unsigned int size:2;
unsigned int delegateAlwaysNotifiesDelegateOfSegmentClicks:1;
unsigned int momentaryClick:1;
unsigned int tracking:1;
unsigned int autosizeToFitSegments:1;
unsigned int isSizingToFit:1;
unsigned int autosizeText:1;
unsigned int transparentBackground:1;
unsigned int useProportionalWidthSegments:1;
unsigned int translucentBackground:1;
unsigned int appearanceNeedsUpdate:1;
} _segmentedControlFlags;
} - (instancetype)initWithItems:(NSArray *)items; // items can be NSStrings or UIImages. control is automatically sized to fit content @property(nonatomic) UISegmentedControlStyle segmentedControlStyle NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect");
@property(nonatomic,getter=isMomentary) BOOL momentary; // if set, then we don't keep showing selected state after tracking ends. default is NO
@property(nonatomic,readonly) NSUInteger numberOfSegments; // For segments whose width value is 0, setting this property to YES attempts to adjust segment widths based on their content widths. Default is NO.
@property(nonatomic) BOOL apportionsSegmentWidthsByContent NS_AVAILABLE_IOS(5_0); - (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated; // insert before segment number. 0..#segments. value pinned
- (void)insertSegmentWithImage:(UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated;
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
- (void)removeAllSegments; - (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment; // can only have image or title, not both. must be 0..#segments - 1 (or ignored). default is nil
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment; - (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment; // can only have image or title, not both. must be 0..#segments - 1 (or ignored). default is nil
- (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment; - (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment; // set to 0.0 width to autosize. default is 0.0
- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment; - (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment; // adjust offset of image or text inside the segment. default is (0,0)
- (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment; - (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; // default is YES
- (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment; // ignored in momentary mode. returns last segment pressed. default is UISegmentedControlNoSegment until a segment is pressed
// the UIControlEventValueChanged action is invoked when the segment changes via a user event. set to UISegmentedControlNoSegment to turn off selection
@property(nonatomic) NSInteger selectedSegmentIndex; /* Default tintColor is nil. The tintColor is inherited through the superview hierarchy. See UIView for more information.
*/
@property(nonatomic,retain) UIColor *tintColor; /* If backgroundImage is an image returned from -[UIImage resizableImageWithCapInsets:] the cap widths will be calculated from that information, otherwise, the cap width will be calculated by subtracting one from the image's width then dividing by 2. The cap widths will also be used as the margins for text placement. To adjust the margin use the margin adjustment methods. In general, you should specify a value for the normal state to be used by other states which don't have a custom value set. Similarly, when a property is dependent on the bar metrics, be sure to specify a value for UIBarMetricsDefault.
In the case of the segmented control, appearance properties for UIBarMetricsCompact are only respected for segmented controls in the smaller navigation and toolbars.
*/
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; /* To customize the segmented control appearance you will need to provide divider images to go between two unselected segments (leftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal), selected on the left and unselected on the right (leftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal), and unselected on the left and selected on the right (leftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected).
*/
- (void)setDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; /* You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in NSAttributedString.h.
*/
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; /* For adjusting the position of a title or image within the given segment of a segmented control.
*/
- (void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @end
2.开关控件
<pre name="code" class="objc"><pre name="code" class="objc">#import "UISwitchVC.h"

@interface UISwitchVC ()

@end

@implementation UISwitchVC

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UISwitch *sw = [[UISwitch alloc]init];
sw.frame = CGRectMake(10, 100, 300, 30);
[sw setOn:YES animated:YES];
[self.view addSubview:sw];
[sw addTarget: self action:@selector(swClick:) forControlEvents:UIControlEventValueChanged]; // Do any additional setup after loading the view.
}
-(void)swClick:(UISwitch *)sw
{
NSLog(@"%d",sw.isOn);
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

3.计数器
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(232, 35, 0);"><span style="color: #777997">#import </span>"UIStepperVC.h"</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(195, 89, 0);"><span style="color: #35568a">@interface</span><span style="color: #000000"> </span>UIStepperVC<span style="color: #000000"> ()</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(53, 86, 138);">@end</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(53, 86, 138);">@implementation<span style="color: #000000"> UIStepperVC</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">- (<span style="color: #35568a">void</span>)viewDidLoad {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(88, 126, 168);"><span style="color: #000000">    [</span><span style="color: #35568a">super</span><span style="color: #000000"> </span>viewDidLoad<span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    <span style="color: #c35900">UIStepper</span> *stepper = [[<span style="color: #c35900">UIStepper</span> <span style="color: #587ea8">alloc</span>]<span style="color: #587ea8">init</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    stepper.<span style="color: #587ea8">frame</span> = <span style="color: #587ea8">CGRectMake</span>(<span style="color: #35568a">10</span>, <span style="color: #35568a">100</span>, <span style="color: #35568a">300</span>, <span style="color: #35568a">30</span>);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    stepper.<span style="color: #587ea8">minimumValue</span> = <span style="color: #35568a">1</span> ;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    stepper.<span style="color: #587ea8">maximumValue</span> = <span style="color: #35568a">100</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    stepper.<span style="color: #587ea8">stepValue</span> = <span style="color: #35568a">2</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    [<span style="color: #35568a">self</span>.<span style="color: #587ea8">view</span> <span style="color: #587ea8">addSubview</span>:stepper];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(88, 126, 168);"><span style="color: #000000">    </span><span style="color: #35568a">self</span><span style="color: #000000">.</span>view<span style="color: #000000">.</span>backgroundColor<span style="color: #000000"> = [</span><span style="color: #c35900">UIColor</span><span style="color: #000000"> </span>whiteColor<span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(88, 126, 168);"><span style="color: #000000">    [stepper </span>setBackgroundImage<span style="color: #000000">:[</span><span style="color: #c35900">UIImage</span><span style="color: #000000"> </span>imageNamed<span style="color: #000000">:</span><span style="color: #e82300">@"2"</span><span style="color: #000000">] </span>forState<span style="color: #000000">:</span>UIControlStateHighlighted<span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(88, 126, 168);"><span style="color: #000000">    [stepper </span>addTarget<span style="color: #000000">:</span><span style="color: #35568a">self</span><span style="color: #000000"> </span>action<span style="color: #000000">:</span><span style="color: #35568a">@selector</span><span style="color: #000000">(stepperClick:) </span>forControlEvents<span style="color: #000000">:</span>UIControlEventValueChanged<span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);"><span style="color: #000000">    </span>// Do any additional setup after loading the view.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">-(<span style="color: #35568a">void</span>)stepperClick:(<span style="color: #c35900">UIStepper</span> *)step</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    <span style="color: #587ea8">NSLog</span>(<span style="color: #e82300">@"%lf"</span>,step.<span style="color: #587ea8">value</span>);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">- (<span style="color: #35568a">void</span>)didReceiveMemoryWarning {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(88, 126, 168);"><span style="color: #000000">    [</span><span style="color: #35568a">super</span><span style="color: #000000"> </span>didReceiveMemoryWarning<span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);"><span style="color: #000000">    </span>// Dispose of any resources that can be recreated.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);">/*</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);">#pragma mark - Navigation</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36); min-height: 16px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);">// In a storyboard-based application, you will often want to do a little preparation before navigation</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);">- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);">    // Get the new view controller using [segue destinationViewController].</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);">    // Pass the selected object to the new view controller.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(207, 135, 36);">*/</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(53, 86, 138);">@end</p><div>
</div>
05-06 07:32