UIView的子类
- UIWindow:
- UILabel:类比于Android中的TextView的显示功能;
- UIPickerView:
- UIProgressView:
- UIActivityIndication:
- UIImageView:
- UITableBar:
- UIToolbar:
- UINavigationBar:
- UITableViewCell:
- UIActionSheet:
- UIAlerView:
- UIScrollView:
- UISearchBar:
- UIWebView:
- UIControl:
1.UILabel类的功能是提供显示及编辑,其属性(跟Android中的属性设置对比学习,原理一样)如下:
@property(nullable, nonatomic,copy) NSString *text; // default is nil
@property(null_resettable, nonatomic,strong) UIFont *font; // default is nil (system font 17 plain)
@property(null_resettable, nonatomic,strong) UIColor *textColor; // default is nil (text draws black)
@property(nullable, nonatomic,strong) UIColor *shadowColor; // default is nil (no shadow)
@property(nonatomic) CGSize shadowOffset; // default is CGSizeMake(0, -1) -- a top shadow
@property(nonatomic) NSTextAlignment textAlignment; // default is NSTextAlignmentNatural (before iOS 9, the default was NSTextAlignmentLeft)
@property(nonatomic) NSLineBreakMode lineBreakMode; // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text
.....
添加一个UILabel的示例:
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 20, 200, 100)];
myLabel.text = @"hello iOS";
myLabel.font = [UIFont systemFontOfSize:20];
myLabel.textColor = [UIColor redColor];
[self.view addSubview:myLabel];
2.UIControl:
UIControl类是一个具有事件处理功能的控件的分类,它包含了三类事件:基于触摸/基于值/基于编辑。
- UIButton:初始化对象时可以选择自己需要的按钮,一般情况下定义为圆角按钮,用户可以自定义按钮如改变按钮背景颜色,为按钮添加图片。
UIButtonType是一个枚举类型的数据类型,枚举中的前两种类型可以自定义按钮在屏幕的位置和按钮大小,但是其他的系统按钮图标大小用户是不能进行自定义的,只能设置它在屏幕中的坐标位置。
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
UIButtonTypePlain API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios, watchos), // standard system button without the blurred background view
UIButtonTypeRoundedRect = UIButtonTypeSystem // Deprecated, use UIButtonTypeSystem instead
};
UIButton的使用示例:
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(120, 270, 100, 30);
button1.backgroundColor = [UIColor whiteColor];
[button1 setTitle:@"view1 top" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(ViewChange1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
其中在UIButton中的forState的属性:
- 正常状态(UIControlStateNormal):系统默认的状态即为正常状态;
- 高亮状态(UIControlStateHighlighted):用户正在使用时组件的状态,如单击一个按钮时就是高亮状态;
- 禁用状态(UIControlStateDisabled):在禁用状态下,用户不能对组件进行操作,要设置按钮的禁用状态,则需要把按钮的Enable属性设为NO;
- 选种状态(UIControlStateSelected):选中状态和高亮状态比较类似,不同的是不用于单击该按钮,它一直会显示为高亮的效果。该状态一般用于指明组件被选中或被打开。
处理事件的功能:
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
// remove the target/action for a set of events. pass in NULL for the action to remove all actions for that target
- (void)removeTarget:(nullable id)target action:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents;
- (id)target:是指定的目标,一般是自己self;
- action:(SEL):是选择要执行的方法,需要用户实现它的功能(与Android中控件执行方法相对比);
- (SEL)forControlEvents:(ControlEvents):是指消息处理时的事件,如单击按钮还是拖动等。
- UITextField:用于外部数据的输入。用来实现人机交互的效果(与Android中的EditText相对应),文本框能设置文本内容/字体颜色大小/占位符还能设置文本框的外框类型:
UITextField *userName = [[UITextField alloc]initWithFrame:CGRectMake(200, 20,300, 50)];
userName.font = [UIFont fontWithName:@"OriyaSangamMN-Bold" size:18.0f];
[userName setBorderStyle:UITextBorderStyleRoundedRect];
userName.clearButtonMode = UITextFieldViewModeAlways;
userName.returnKeyType = UIReturnKeyDone;
userName.placeholder = @"输入用户名字";
[self.view addSubview:userName];
setBorderStyle属性时设置外边框的类型,clearButtonMode属性是设置清除按钮出现的时间,returnkeyType属性用于设置按钮的类型。
- UISlider:一般用于系统声音/亮度等设置(与Android中SeekBar相对应)。
slider = [[UISlider alloc]initWithFrame:CGRectMake(200, 40,300, 50)];
[slider setMaximumValue:100];
[slider setMinimumValue:0];
[slider addTarget:self action:@selector(getValue1) forControlEvents:UIControlEventValueChanged];
slider.value = 50;
[self.view addSubview:slider];
-(void) getValue1
{
int intVaule = (int)slider.value;
NSString *stringVaule = [[NSString alloc]initWithFormat:@"%d",intVaule];
myLabel.text = stringVaule;
[stringVaule release];
}
- UISegmentedControl和UIPageControl:主要功能用于不同类型信息的选择和在不同屏幕间切换(与Android中的TabHost相对应)
NSArray *item = @[@"分页1",@"分页2",@"分页3"];
segmentControl = [[UISegmentedControl alloc]initWithItems:item];
segmentControl.frame = CGRectMake(230,200,300, 50);
segmentControl.segmentedControlStyle = UIProgressViewStyleBar;
[segmentControl setWidth:30.0f forSegmentAtIndex:1];
[segmentControl addTarget:self action:@selector(selectedSegmentIndex) forControlEvents:UIControlEventValueChanged];
segmentControl.selectedSegmentIndex = 1;
-(void) selectSegmentIndex:(UISegmentedControl *)segmented
{
NSString *labeltext = [[NSString alloc]initWithFormat:@"现在选择的是%d",segmented.selectedSegmentIndex];
myLabel.text = labeltext;
[labeltext release];
}
- UIActivityIndicatorView:效果是现实载入等待画面的进度圈(与Android 中的ProgressBar相对应),属性setNetworkActvivityIndicatorVisible来进行显示隐藏;
- UIAlertView和UIActionSheet(与Android中Dialog相对应):
UIAlertView运用了UIAlertViewDelegate代理方法进行事件的处理,UIAlertView和UIActionSheet视图级别非常高,在程序运行下当显示视图时,其他事件都被阻断,单击其他区域无效,必须要完成视图的事件才能进行继续其他事件。
UIAlertView视图继承于UIView,所以在UIAlertView上添加子视图的方法和其他视图一致。
UIAlertView实例显示时它时Application的第一响应者,也就是FistReponder,只有当用户选择相应的按钮之后才可以进行其他操作