百思不得姐

来源:互联网 发布:canvas性能优化 编辑:程序博客网 时间:2024/04/28 16:29

百思不得姐

  • 1.图标添加,添加启动图片
    • launch screen file删掉
    • info Bundle name中改名字
  • 2.设置窗口根控制器
    • 把main.storyboard和xib都删除,Main interface删除
    • 在程序启动的时候调用的方法 (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions中创建启动控制器。
      -分析我们app的控制器是哪种类型的,一般都是TabBarController

    • //设置窗口
      self.window=[[UIWindow alloc]init];
      self.window.frame=[UIScreen mainScreen].bounds;
      //设置窗口的根控制器
      self.window.rootViewController=[[ViewController alloc]init];
      //显示窗口
      [self.window makeKeyAndVisible];
  • 3.搭建骨架
    • tabBarController里添加子控制器
    self.window=[[UIWindow alloc]init];    self.window.frame=[UIScreen mainScreen].bounds;    //设置窗口的根控制器    UITabBarController*tabBarController=[[UITabBarController  alloc]init];    //添加子控制器    UIViewController*vc01=[[UIViewController alloc]init];    vc01.view.backgroundColor=[UIColor redColor];    [tabBarController addChildViewController:vc01];    UIViewController*vc02=[[UIViewController alloc]init];    vc02.view.backgroundColor=[UIColor blueColor];    [tabBarController addChildViewController:vc02];    UIViewController*vc03=[[UIViewController alloc]init];    vc03.view.backgroundColor=[UIColor greenColor];    [tabBarController addChildViewController:vc03];    UIViewController*vc04=[[UIViewController alloc]init];    vc04.view.backgroundColor=[UIColor grayColor];    [tabBarController addChildViewController:vc04];    self.window.rootViewController=tabBarController;    //显示窗口    [self.window makeKeyAndVisible];
  • 4.设置四个子控制器的title和image,注意,这个tabBarItem属性是控制器的属性
//添加子控制器    UIViewController*vc01=[[UIViewController alloc]init];    vc01.tabBarItem.title=@"精华";    vc01.tabBarItem.image=[UIImage imageNamed:@"tabBar_essence_icon"];    vc01.tabBarItem.selectedImage=[UIImage imageNamed:@"tabBar_essence_click_icon"];    vc01.view.backgroundColor=[UIColor redColor];    [tabBarController addChildViewController:vc01];    UIViewController*vc02=[[UIViewController alloc]init];    vc02.tabBarItem.title=@"新帖";    vc02.tabBarItem.image=[UIImage imageNamed:@"tabBar_new_icon"];    vc02.tabBarItem.selectedImage=[UIImage imageNamed:@"tabBar_new_click_icon"];    vc02.view.backgroundColor=[UIColor redColor];    [tabBarController addChildViewController:vc02];    UIViewController*vc03=[[UIViewController alloc]init];    vc03.tabBarItem.title=@"关注";    vc03.tabBarItem.image=[UIImage imageNamed:@"tabBar_friendTrends_icon"];    vc03.tabBarItem.selectedImage=[UIImage imageNamed:@"tabBar_friendTrends_click_icon"];    vc03.view.backgroundColor=[UIColor redColor];    [tabBarController addChildViewController:vc03];    UIViewController*vc04=[[UIViewController alloc]init];    vc04.tabBarItem.title=@"我";    vc04.tabBarItem.image=[UIImage imageNamed:@"tabBar_me_icon"];    vc04.tabBarItem.selectedImage=[UIImage imageNamed:@"tabBar_me_click_icon"];    vc04.view.backgroundColor=[UIColor redColor];    [tabBarController addChildViewController:vc04];
  • 5.为了增加代码的扩展性,自定义TabBarController,然后把tabBarController里增加子控件的代码封装在自定义TabBarController里面

  • 6.解决图片被渲染的问题

  • 7.解决文字颜色问题,

    • 通过代码直接设置

      UIViewController*vc01=[[UIViewController alloc]init];vc01.view.backgroundColor=[UIColor redColor];vc01.tabBarItem.title=@"精华";NSMutableDictionary*attrs=[NSMutableDictionary dictionary];attrs[NSFontAttributeName]=[UIFont systemFontOfSize:19];attrs[NSForegroundColorAttributeName]=[UIColor grayColor];[vc01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];NSMutableDictionary*selectedAttrs=[NSMutableDictionary dictionary];selectedAttrs[NSFontAttributeName]=[UIFont systemFontOfSize:10];selectedAttrs[NSForegroundColorAttributeName]=[UIColor greenColor];[vc01.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];vc01.tabBarItem.image=[UIImage imageNamed:@"tabBar_essence_icon"];vc01.tabBarItem.selectedImage=[UIImage imageNamed:@"tabBar_essence_click_icon"];
    • 通过appearance统一设置UITabBarItem的文字属性。

//拿到appearence,同意设置选中和未选中的文字大小及颜色  NSMutableDictionary*attrs=[[NSMutableDictionary alloc]init];    attrs[NSFontAttributeName]=[UIFont systemFontOfSize:12];    attrs[NSForegroundColorAttributeName]=[UIColor grayColor];    NSMutableDictionary*selectedAttrs=[[NSMutableDictionary alloc]init];    selectedAttrs[NSFontAttributeName]=[UIFont systemFontOfSize:12];    selectedAttrs[NSForegroundColorAttributeName]=[UIColor grayColor];    UITabBarItem*item=[UITabBarItem appearance];    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
  • 7.提取创建子控制器代码方法,并且由于要创建拆不同的控制器
   [self setupChildVc:[[UITableViewController alloc]init] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];    [self setupChildVc:[[UITableViewController alloc]init] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];    [self setupChildVc:[[UITableViewController alloc]init] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];    [self setupChildVc:[[UITableViewController alloc]init] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];}-(void)setupChildVc:(UIViewController*)vc title:(NSString*)title image:(NSString*)image selectedImage:(NSString*)selectedImage{    vc.tabBarItem.title=title;    vc.tabBarItem.image=[UIImage imageNamed:image];    vc.tabBarItem.selectedImage=[UIImage imageNamed:selectedImage];    //设置随机颜色    vc.view.backgroundColor=[UIColor colorWithRed:arc4random_uniform(100)/100.0 green:arc4random_uniform(100)/100.0 blue:arc4random_uniform(100)/100.0 alpha:1.0];    [self addChildViewController:vc];}
  • 8.快速的按照App需要分好模块。因为系统的控制器UITableViewController不能满足我们开发的需求,需要自定义控制器.TabBarController中创建的自控制器换成我们自己自定义的控制器.这样也非常有利于我们分模块开发

-9 .因为tabbar不满足我们的要求,我们目标是中间有一个加号的可以点击的控件,所以我们需要自定义tabbar

  • //tabBar和tabBarItem的区别?控制器也有vc.tabBarItem.title=title;tabBarItem属性。tabbar是底部的标签条,是一个view。 UIControlStateHighlighted和UIControlStateSelected区别

    • 因为tabBar是UITabBarController中的一个属性
[self setValue:[[XMGTabBar alloc]init] forKeyPath:@"tabBar"];//把自定义的TabBar对象赋值给属性


- 所以直接新建一个TabBar类,直接继承自UITabBar,然后更换TabBar,一般如果是更换的话可以如图

- 但是会报错,因为属性的readonly,所以要用KVC,会自动找到成员变量赋值

//更换TabBar
[self setValue:[[XMGTabBar alloc]init] forKeyPath:@"tabBar"];

  • 这里还有要注意的是,当- (void)viewDidLoad时我们NSLog(@”%@”,self.tabBar.subviews);结果是 “
  -(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    NSLog(@"%@",self.tabBar.subviews);}
"<UIButton: 0x7fdc594627e0; frame = (0 0; 49 49); opaque = NO; layer = <CALayer: 0x7fdc59462cc0>>",    "<UITabBarButton: 0x7fdc595c6e00; frame = (2 1; 90 48); opaque = NO; layer = <CALayer: 0x7fdc59507b80>>",    "<UITabBarButton: 0x7fdc595cda90; frame = (96 1; 90 48); opaque = NO; layer = <CALayer: 0x7fdc595ce090>>",    "<UITabBarButton: 0x7fdc595d21d0; frame = (190 1; 89 48); opaque = NO; layer = <CALayer: 0x7fdc595066e0>>",    "<UITabBarButton: 0x7fdc59626c10; frame = (283 1; 90 48); opaque = NO; layer = <CALayer: 0x7fdc59626f80>>"

说明这个时候四个子控件已经出来了而且尺寸也能看到几乎是90*4;

  • 10.然后就可以自定义我们的TabBar,由于通过代码自定义控件,所以重写initWithFrame:(CGRect)frame方法,

    -(instancetype)initWithFrame:(CGRect)frame{if(self==[super initWithFrame:frame]){    UIButton*publishButton=[UIButton buttonWithType:UIButtonTypeCustom];    [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon" ] forState:UIControlStateNormal];    [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon" ]forState:UIControlStateHighlighted];    [self addSubview:publishButton];    self.publishButton=publishButton;}return self;}

    并且设置frame的方法要在ayoutSubviews方法中写,因为 -(instancetype)initWithFrame:(CGRect)frame中拿到的frame是假的,会自动调-(void)layoutSubviews方法,所以我们要重写-(void)layoutSubviews方法

    -(void)layoutSubviews{[super layoutSubviews];//先调用父类的layoutSubviews,布局完我们再重新布局,覆盖掉frame//设置发布按钮frameself.publishButton.bounds=CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);self.publishButton.center=CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5);CGFloat buttonY=0;CGFloat buttonW=self.frame.size.width/5;CGFloat buttonH=self.frame.size.height;NSInteger index=0;//设置其他按钮,遍历出tabbar的子控件for (UIView*button in self.subviews) {    //对子控件进行判断    if(![button isKindOfClass:NSClassFromString(@"UITabBarButton") ])continue;    CGFloat buttonX=buttonW*((index<=1)?index:(index+1));    button.frame=CGRectMake(buttonX, buttonY, buttonW, buttonH);    index++;}}
    • 11.由于程序中经常需要修改frame,所以我们需要对frame进行封装,给所有的UI控件加一个分类,而且这个分类(分类只能扩充方法,类扩展才能扩充属性,所以我们需要重写set和get方法的实现)在分类中声明@property,只会生成方法的声明,不会生成方法的实现和带有下划线的成员变量,是项目中每个都用得到的所以建一个pch,Build Settings中查找Prefix Header,Prefix Header中双击放pch的路径


    • 12.包装导航控制器

-(void)setupChildVc:(UIViewController*)vc title:(NSString*)title image:(NSString*)image selectedImage:(NSString*)selectedImage{    vc.navigationItem.title=title;    vc.tabBarItem.title = title;    vc.tabBarItem.image = [UIImage imageNamed:image];    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];    //包装一个导航控制器    XMGNavigationController*nav=[[XMGNavigationController alloc]initWithRootViewController:vc];//把创建的vc控制器扔到导航控制器里    [self addChildViewController:nav];}
  • 13.设置导航栏

  • 设置导航栏标题的文字属性
    “`
    vc.navigationItem.title=title;
 - 分别在各个控制器内部完善导航栏![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160820_15.png)![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160820_16.png)![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160820_17.png) - 14.封装创建uibarbuttonitem也就是导航栏按钮   - 直接给uibarbuttonitem增加一个分类,这样不会产生新的类。

+(instancetype)itemWithImage:(NSString*)image highImage:(NSString*)highImage target:(id)target action:(SEL)action
{

UIButton*butoon=[UIButton buttonWithType:UIButtonTypeCustom];[butoon setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];[butoon setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];butoon.size=butoon.currentBackgroundImage.size;[butoon addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];return [[self alloc]initWithCustomView:butoon];

}

然后再各个控制器中就可以这样创建导航栏按钮了

//设置导航栏右边按钮

UIBarButtonItem*settingItem=[UIBarButtonItem itemWithImage:@"mine-setting-icon" highImage:@"mine-setting-icon-click" target:self action:@selector(settingClick)];UIBarButtonItem*moonItem=[UIBarButtonItem itemWithImage:@"mine-moon-icon" highImage:@"mine-moon-icon-click" target:self action:@selector(moonClick)];self.navigationItem.rightBarButtonItems=@[                                          settingItem,                                          moonItem                                          ];//注意添加顺序,从右往左self.view.backgroundColor=XMGGlobalBg;
- 15.调整项目结构![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160820_19.png)- 16.设置统一背景色,在每个对应模块中设置,避免控制器被提前创建。因为如果在自定义的tabbarcontroller中设置要拿到view属性去设置,会导致控制器被提前创建![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160821_1.png)- 17.设置tabbar的背景图片在更换完tabbar之后设置,但是tabbar是自定义的所以直接封装在自定义的tabbar里不需要让外界知道

[self.tabBar setBackgroundImage:[UIImage imageNamed:@”tabbar-light”]];

![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160821_2.png)- 18.设置导航条的背景图片![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160821_3.png)- 19.自定义导航控制器因为不自定义的话导航栏的返回键标题就是导航栏的标题,所以为了避免标题过长要自定义怎么自定义呢,因为我们是调用

[self.navigationController pushViewController:vc animated:YES];

这个方法push到下一个控制器,所以我们需要自定义导航栏控制器,拦截pushViewController方法,也就是在这个方法里自定义

//设置导航控制器返回按钮样式
viewController.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@”返回” style:UIBarButtonItemStyleDone target:nil action:nil];
“`
- 这个很多知识点很重要,自己看注释

   #import "XMGNavigationController.h"   @interface XMGNavigationController ()   @end   @implementation XMGNavigationController
- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    }- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{    if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];        [button setTitle:@"返回" forState:UIControlStateNormal];        [button setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];        [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];        button.size = CGSizeMake(70, 30);        // 让按钮内部的所有内容左对齐        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;        //        [button sizeToFit];        // 让按钮的内容往左边偏移10        button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];        //由于自己定义的导航栏控制器,所以要自己设置点击返回        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];        // 隐藏tabbar        viewController.hidesBottomBarWhenPushed = YES;    }    // 这句super的push要放在后面,因为我们不需要开始界面的各个控制器成为左边返回样式的,而开始的四个控制器也是push进去的第一个控制器,所以我们要后面调用[super pushViewController:viewController animated:animated] 让viewController可以覆盖上面设置的leftBarButtonItem    [super pushViewController:viewController animated:animated];}- (void)back{    [self popViewControllerAnimated:YES];}@end
  • 20.调整初始化代码
    设置导航条的背景图片的代码
 //设置导航条的背景图片    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];

挪到我们自定义的导航控制器里,当viewDidLoad之后

 //设置导航条的背景图片    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];

  • 同样把在XMGTabBarController中设置appearence的一次性设置放在+(void)initialize中
    “`
    +(void)initialize
    {
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];//文字大小
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];//文字颜色

    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];

    UITabBarItem*item=[UITabBarItem appearance];//设置item,所有的tabbaritem都一样,相当于设置他的皮肤,因为setTitleTextAttributes:attrs forState:UIControlStateNormal方法后面有appearance宏,凡是后面有appearance的都可以统一设置
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    }

- 21.完善关注控制器-Xib方式创建,save as选中关注控制器,告诉xib这个view是哪个控制器的view,所以File's Owner的Custom Class中Class得是我们的控制器,意思是这个文件是为我们的控制器服务的。右键File's Owner把view属性连到view。*storyboard或者xib中label文字换行要按住option键敲回车才行  - label只要设置宽度,高度会自动计算。  - 自定义一个控制器,当点击左上角的按钮时,push到我们自定义的控制器

-(void)friendsClick
{
XMGRecommendViewController*vc=[[XMGRecommendViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];

}

- 显示推荐关注,用到的第三方框架afn发请求给服务器把jason给我,sdwebimage- 安装第三方框架管理工具(参见10)

sudo gem install cocoapods

pod repo remove master

pod repo add master https://gitcafe.com/akuandev/Specs.git

pod repo add master http://gitoschina.net/akuandev/Specs.git
pod repo update

- 22.推荐关注点击关注界面的左上角的按钮,显示推荐关注  - 1.push到下一个控制器,由于是左边显示关注类型,右边是列表,所以用一个继承自uiviewcongtroller的控制器记好了

-(void)friendsClick
{
XMGRecommendViewController*vc=[[XMGRecommendViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}

 新的控制器标题改一成推荐关注![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160823_1.png)  -显示指示器   ```   //显示指示器    [SVProgressHUD show];    ```  - 给服务器发送请求,获得数据  ```  //发请求给服务器    NSMutableDictionary*params=[NSMutableDictionary dictionary];    params[@"a"]=@"category";    params[@"c"]=@"subscribe";    //IOS9.0之后协议的Url改为https开头,切记    [[AFHTTPSessionManager manager]GET:@"https://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {        nil;    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        //根据返回结果的JSON示例,可以知道responseObject就是个字典        NSLog(@"%@",responseObject);        [SVProgressHUD dismiss];    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {        [SVProgressHUD showErrorWithStatus:@"加载失败"];    }];
  • 在XMGRecommendViewController的xib中加入tableView,设置tableView数据源以及代理为File’s Owner(控制器)
  • 控制器遵守代理协议和数据源协议
   @interface XMGRecommendViewController ()<UITableViewDelegate,UITableViewDataSource>
  • 写好数据源方法,在XMGRecommendViewController控制器里增加一个属性存放从服务器返回的数据
/**左边的类别数据*/@property(nonatomic,strong)NSArray*categories;
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.categories.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    XMGRecommendCategoryCell*cell=[tableView dequeueReusableCellWithIdentifier:XMGCategoryId];    cell.category=self.categories[indexPath.row];    return cell;}
  • 在关注模块里创建模型,设置模型中对应的属性
#import <Foundation/Foundation.h>@interface XMGRecommendCategory : NSObject@property(nonatomic,assign)NSInteger id;@property(nonatomic,assign)NSInteger count;@property(nonatomic,copy)NSString* name;@end

在控制器里设置类别数据

/**左边的类别数据*/@property(nonatomic,strong)NSArray*categories;/**左边的类别表格*/@property (weak, nonatomic) IBOutlet UITableView *categoryTableView;

把服务器返回的数据转模型赋值给categories

        self.categories=[XMGRecommendCategory mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];        //刷新表格        [self.categoryTableView reloadData];        //设置默认显示第一行        [self.categoryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
  • 把模型赋值给cell属性
-(void)setCategory:(XMGRecommendCategory *)category{    //模型赋值给属性    _category=category;    self.textLabel.text=category.name;}
  //因为cell是从xib中来的,所以会调用这个方法,在这里自定义cell- (void)awakeFromNib {    [super awakeFromNib];    // Initialization code   self.backgroundColor=XMGRGBColor(244, 244, 244);}
  • 去XMGRecommendViewController中把tableView背景色清除
    设置selection(分隔线)为No Selection.往cell里拖一个uiview,从写layoutSubviews自定义cell内部子控件
 //自定义cell内部子控件-(void)layoutSubviews{   [super layoutSubviews];    self.textLabel.height=self.contentView.height-2;} ```- 当cell被用户选中时会进入并且传入一个YES

//当cell被用户选中时会进入并且传入一个YES
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
//没有被用户选中时,这个控件隐藏
self.selectedIndicator.hidden=!selected;
//设置选中和没选中时textColor
self.textLabel.textColor=selected?[UIColor redColor]:XMGRGBColor(78, 78, 78);
}

- 23.推荐关注→_

//当cell被用户选中时会进入并且传入一个YES,可以在这个方法中监听cell的选中和取消选中
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
//没有被用户选中时,这个控件隐藏
self.selectedIndicator.hidden=!selected;
//设置选中和没选中时textColor
self.textLabel.textColor=selected?[UIColor redColor]:XMGRGBColor(78, 78, 78);
}

 - 先在xib中加入右边的tableView。 我们肯定是要用到的所以连号线

/*右边的用户表格/
@property (weak, nonatomic) IBOutlet UITableView *userTableView;

- 快速完成右边的usertableView,自定义一个XMGRecommendUserCell,在xib中完善cell。拖好image label 和button设置好约束,label和button一般不需要设置宽度,会自动包裹。然后identifier设置成user。 - 同左侧类似

static NSString* const XMGUserId=@”user”;
//从nib文件加载自定义的Cell
[self.userTableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGRecommendUserCell class]) bundle:nil] forCellReuseIdentifier:XMGUserId];

 - 设置右侧用户数据的数据源方法为控制器,就是按住ctrl右键连线datasource。
  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    //由于是两个tableView共用一个数据源方法,都是这个控制器,所以这里需要进行判断
    if(tableView==self.categoryTableView){
    return self.categories.count;
    }else {
    return 0;//这里暂时设置为0;
    }

}
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//同理这里也需要进行判断
if(tableView==self.categoryTableView){

XMGRecommendCategoryCell*cell=[tableView dequeueReusableCellWithIdentifier:XMGCategoryId];cell.category=self.categories[indexPath.row];    return cell;}else{    XMGRecommendUserCell*cell=[tableView dequeueReusableCellWithIdentifier:XMGUserId];    return cell;}

}

- 24.登录界面  - 创建XMGLoginRegisterViewController,同时创建Xib  - 创建好对应的button,label,imageview,view  - 在button中由于imageView和label是左右排布的,不符合我们的要求,我们要求垂直排布,所以我们需要自定义一个uibutton,所以我们自定义XMGVerticalButton,继承自uibutton;然后再内部重写-(void)layoutSubviews和-(void)awakeFromNib

import “XMGVerticalButton.h”

@implementation XMGVerticalButton

-(void)awakeFromNib
{
//文字居中对齐
self.titleLabel.textAlignment=NSTextAlignmentCenter;
}
-(void)layoutSubviews
{
[super layoutSubviews];
//调整图片
self.imageView.x=0;
self.imageView.y=0;
self.imageView.width=self.width;
self.imageView.height=self.imageView.width;
//调整文字
self.titleLabel.x=0;
self.titleLabel.y=self.imageView.height;
self.titleLabel.width=self.width;
self.titleLabel.height=self.height-self.titleLabel.y;

}
@end

 - 然后再xib中把对应的button类名改为我们自定义的。![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160905_1.png)- 登录按钮的圆角处理 ```  self.loginButton.layer.cornerRadius=5;  //让所有的层    self.loginButton.layer.masksToBounds=YES; ```  - 不用代码的处理圆角方式:在xib中直接用KVC更改  ![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160906_1.png)- 占位文字颜色

//文字属性
NSMutableDictionary*attrs=[NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName]=[UIColor grayColor];
//NSAttributedString:带有属性的文字(富文本技术)
NSAttributedString*placeholder=[[NSAttributedString alloc]initWithString:@”手机号” attributes:attrs];
self.phoneField.attributedPlaceholder=placeholder;

- 另一种方法就是自定义textfield

-(void)drawPlaceholderInRect:(CGRect)rect
{
[super drawPlaceholderInRect:rect];
[self.placeholder drawInRect:CGRectMake(0, 10, rect.size.width, 25) withAttributes:@{
NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:self.font
}];
}

- 文本框清楚按钮以及密码安全 ![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160907_1.png) - 注册账号动画,顺便设置按钮文字  - 拿到左边的约束,拖线 ![](file:///Users/xiaoxinping/Desktop/截图/7-3/Snip20160907_2.png) ```- (IBAction)showLoginOrRegister:(UIButton*)button {     -  //退出键盘        [self.view endEditing:YES];    if(self.loginViewLeftMargin.constant==0){    //左移一个宽度        self.loginViewLeftMargin.constant=-self.view.width;}    else if (self.loginViewLeftMargin.constant==-self.view.width){        self.loginViewLeftMargin.constant=0;        [button setTitle:@"已有账号" forState:UIControlStateNormal];    }    //0.25s后布局,有动画效果    [UIView animateWithDuration:0.25 animations:^{        [self.view layoutIfNeeded];    }];}} ``` - 点击返回,把控制器推下去 ``` - (IBAction)back:(id)sender {    [self dismissViewControllerAnimated:YES completion:nil];} ``` - 22.精华控制器  - 设置顶部的标签栏

//设置顶部的标签栏
-(void)setupTitlesView
{
UIView*titlesView=[UIView alloc];
titlesView.backgroundColor=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
titlesView.width=self.view.width;
titlesView.height=35;
titlesView.y=64;
[self.view addSubview:titlesView];
NSArray*titles=@[@”全部”,@”全部”,@”全部”,@”全部”,@ “全部”];
for (NSInteger i=0;i

0 0
原创粉丝点击