您的位置:首页 > 移动开发 > IOS开发

ios创建视图的一些方法封装

2015-10-16 13:56 519 查看
<pre class="objc" name="code">//get请求
-(void)getRequestWithString:(NSString *)urlStr andParams:(NSDictionary *)params andAcceptableContentTypes:(NSSet *)acceptableContentTypes
{
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
manager.requestSerializer=[AFJSONRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes=acceptableContentTypes;
[manager GET:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"网络请求失败:%@",error);
}];
}

//post请求
-(void)postRequestWithString:(NSString *)urlStr andParams:(NSDictionary *)params andAcceptableContentTypes:(NSSet *)acceptableContentTypes
{
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes=acceptableContentTypes;
manager.requestSerializer=[AFJSONRequestSerializer serializer];
[manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"网络请求失败:%@",error);
}];
}

//是否隐藏键盘
-(void)hiddenKeyboard:(BOOL)yesOrNo WithTextfield:(UITextField *)textield
{
if (yesOrNo==NO) {
return;
}
[textield resignFirstResponder];
NSInteger offset=CGRectGetMaxY(textield.frame)-(HEIGHT-216.0);
if (offset>0) {
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame=CGRectMake(0, -offset, WIDTH, HEIGHT);
}
[UIView commitAnimations];
}

//创建按钮
-(UIButton *)createButtonWithButtonType:(UIButtonType)buttonType andFrame:(CGRect)frame andSelecet:(SEL)selector andTitle:(NSString *)title andTitleColor:(UIColor *)titleColor andBackgroundColor:(UIColor *)backgroundColor andNormalImage:(UIImage *)normalIcon andSelectedImage:(UIImage *)selectedIcon
{
UIButton *btn=[UIButton buttonWithType:buttonType];
btn.frame=frame;
[btn setBackgroundColor:backgroundColor];
[btn setTitleColor:titleColor forState:UIControlStateNormal];
[btn setBackgroundImage:normalIcon forState:UIControlStateNormal];
[btn setBackgroundImage:selectedIcon forState:UIControlStateSelected];
[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
return btn;
}

//创建label
-(UILabel *)createLabelWithFrame:(CGRect)frame andTitle:(NSString *)title andFont:(UIFont *)font andMaskToBounds:(BOOL)maskToBounds
{
UILabel *label=[[UILabel alloc] initWithFrame:frame];
label.text=title;
label.font=font;
if (maskToBounds==YES) {
label.layer.cornerRadius=5.0;
label.layer.masksToBounds=YES;
}
return label;
}

//创建textField
-(UITextField *)createTextfieldWithFrame:(CGRect)frame andPlaceholder:(NSString *)placeholder andDelegate:(id)delegate
{
UITextField *textField=[[UITextField alloc] initWithFrame:frame];
textField.placeholder=placeholder;
textField.delegate=delegate;
return textField;
}

//创建textView
-(UITextView *)createTextViewWithFrame:(CGRect)frame andDelegate:(id)delegate
{
UITextView *textView=[[UITextView alloc] initWithFrame:frame];
textView.delegate=delegate;
return textView;
}

//创建ImageView
-(UIImageView *)createImageViewWithFrame:(CGRect)frame andImage:(UIImage *)icon
{
UIImageView *iconView=[[UIImageView alloc] initWithFrame:frame];
iconView.image=icon;
return iconView;
}

//创建tableView
-(UITableView *)createTableViewWithFrame:(CGRect)frame andDataSource:(id)dataSource andDelegate:(id)delegate
{
UITableView *tableView=[[UITableView alloc] initWithFrame:frame];
tableView.dataSource=dataSource;
tableView.delegate=delegate;
return tableView;
}

//创建scrollView
-(UIScrollView *)createScrollViewWithFrame:(CGRect)frame andDelegate:(id)delegate andContentSize:(CGSize)size andShowsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator andShowsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator andPagingEnabled:(BOOL)pagingEnabled
{
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:frame];
scrollView.delegate=delegate;
scrollView.contentSize=size;
scrollView.showsHorizontalScrollIndicator=showsHorizontalScrollIndicator;
scrollView.showsVerticalScrollIndicator=showsVerticalScrollIndicator;
scrollView.pagingEnabled=pagingEnabled;
return scrollView;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: