美文网首页iOS开发
知识点汇总(1)

知识点汇总(1)

作者: 羊驼先生丶 | 来源:发表于2016-08-25 15:06 被阅读43次

    总结的知识点

    1.APP名字的更改

    首先在info中找到Bundle name,改为自己的名字

    2.APP图标的更改
    在General中找到APPIcons and Launch Images更改APP icons source 将图标加到APPicon文件中需要将launch screen file 选项里面去掉,更改launch image source 将启动页图片加入Launchimage

    3.UInavigationBar的appearance方法的使用

    // 通过appearance统一设置所有UITabBarItem的文字属性

    // 后面带有UI_APPEARANCE_SELECTOR的方法, 都可以通appearance对象来统一设置
    
    
    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 setTitleTextAttributes:attrs forState:UIControlStateNormal];
    
        [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    

    4.UItarBarController初始化子控制器的封装
    ,需要继承自
    UItarBarController

    
    - (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];
    
        // 添加为子控制器
    
       // [self addChildViewController:vc];
    
       // 包装一个导航控制器
    , 添加导航控制器为
    tabbarcontroller的子控制器
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [self addChildViewController:nav];
    }
    

    5.自定义
    TarBar控件的子视图的布局是在方法中实现的

    - (void)layoutSubviews
    {
      [super layoutSubviews];
     }
    

    定制TarBar上自带的button的位置需要以下代码

    
    - (void)layoutSubviews
    {
       [super layoutSubviews];
    
        for (UIView *button in self.subviews) {
    
        //设置其他
    UITabBarButton的frame不包括自定义的那个按钮
    
         if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        }
    }
    

    6.分类中属性的创建分类的创建source 找到objective-C File 选择category
    在分类中声明@property, 只会生成方法的声明, 不会生成方法的实现和带有_下划线的成员变量需要自己创建setter方法和getter方法

    
    #import <UIKit/UIKit.h>
    @interface UIView (XMGExtension)
    
    @property (nonatomic, assign) CGFloat width;
    
    @property (nonatomic, assign) CGFloat height;
    
    @property (nonatomic, assign) CGFloat x;
    
    @property (nonatomic, assign) CGFloat y; 
    
    
    #import "UIView+XMGExtension.h"
    
    @implementation UIView (XMGExtension)
    
    - (void)setWidth:(CGFloat)width
    {
        CGRect frame = self.frame;
        frame.size.width = width;
        self.frame = frame;
    }
    - (void)setHeight:(CGFloat)height
    {
        CGRect frame = self.frame;
    
        frame.size.height = height;
    
        self.frame = frame;
    
    }
    - (void)setX:(CGFloat)x
    {
        CGRect frame = self.frame;
        frame.origin.x = x;
        self.frame = frame;
    }
    - (void)setY:(CGFloat)y
    {
        CGRect frame = self.frame;
        frame.origin.y = y;
        self.frame = frame;
    }
    - (CGFloat)width
    {
        return self.frame.size.width;
    }
    - (CGFloat)height
    {
        return self.frame.size.height;
    }
    - (CGFloat)x
    {
        return self.frame.origin.x;
    }
    - (CGFloat)y
    {
       return self.frame.origin.y;
    }
    
    @end
    

    分类中属性创建也可以用runtime实现

    
    #import <UIKit/UIKit.h>
    
    typedef void(^completionhandler)();
    
    @interface UIControl (addBlock)
    
    -(void)addActionforControlEvents:(UIControlEvents )controlEvent withcompletionhandler:(completionhandler)handler;
    @end
    
    
    #import "UIControl+addBlock.h"
    
    #import <objc/runtime.h>
    
    static void *blockKye=@"blockKye";
    
    @implementation UIControl (addBlock)
    
    -(void)addActionforControlEvents:(UIControlEvents )controlEvent withcompletionhandler:(completionhandler)handler{
    
        [self addTarget:self action:@selector(block) forControlEvents:controlEvent];
    
        void(^block)()=^{
            handler();
        };
        objc_setAssociatedObject(self, blockKye, block, OBJC_ASSOCIATION_COPY);
    
    }
    -(void)block{
    
        void (^block)()=objc_getAssociatedObject(self, blockKye);
    
        block();
    
    }
    @end
    
    

    7.PCH文件的创建

    新建一个pch,在other中找到PCH File
    在build Settings 中找到prefix Header 修改文件路径

    8.添加调试的日志输出宏添加到pch文件中

    
    #ifdef DEBUG
    //打印日志
    #define AZLog(...) NSLog(__VA_ARGS__)
    
    #else
    
    #define AZLog(...)
    
    #endif
    
    //打印函数
    
    #define AZLogFunc AZLog(@"%s", __func__)
    #endif
    

    9.UIBarButtonItem的封装 给他创建分类

    
    #import <UIKit/UIKit.h>
    @interface UIBarButtonItem (XMGExtension)
    
    + (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action;
    
    @end
    
    /////////////////////////////////////
    
    #import "UIBarButtonItem+XMGExtension.h"
    @implementation UIBarButtonItem (XMGExtension)
    
    + (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action
    
    {
    
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
        [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    
        [button setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    
        button.size = button.currentBackgroundImage.size;
    
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
        return [[self alloc] initWithCustomView:button];
    
    }
    
    @end
    

    设置导航栏左边的按钮

    
    // 设置导航栏左边的按钮
    
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"imageName" highImage:@"higeImageName" target:self action:@selector(yourClincked)];
    
    

    10.颜色创建的宏 放在pch文件中

    
    #define AZRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
    
    

    11.通过继承
    UINavigationController重写方法

    /*- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

    可以在这个方法中拦截所有push进来的控制器下面代码定制全局的返回键

    
    - (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];
    
            // 隐藏
    tabbarviewController.hidesBottomBarWhenPushed = YES;
        }
        // 这句
    super的push要放在后面, viewController可以覆盖上面设置的leftBarButtonItem
    
        [super pushViewController:viewController animated:animated];
    
    }
    - (void)back
    {
        [self popViewControllerAnimated:YES];
    }
     
    

    12.cell是否选中,选中后的回调方法,可以用于选中某个cell后cell的视图变化,比如选中某个cell的时候字体的颜色改变

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];    
        self.selectedIndicator.hidden = !selected;
    
        self.textLabel.textColor = selected ? self.selectedIndicator.backgroundColor : AZRGBColor(78, 78, 78);
    }
    
    

    相关文章

      网友评论

        本文标题:知识点汇总(1)

        本文链接:https://www.haomeiwen.com/subject/ehkasttx.html