1. 设置导航条背景图片的时候forBarMetrics枚举的取值
设置导航条背景图片
[navigationBar setBackgroundImage:[UIImage imageNamed: @"NavBar64" ] forBarMetrics:UIBarMetricsDefault];
设置导航条背景的时候forBarMetrics:枚举的取值
1 >UIBarMetricsDefault:只有UIBarMetricsDefault枚举才能设置导航条的背景图片
2 >UIBarMetricsCompact:除了UIBarMetricsDefault,其他UIBarMetricsCompact等等枚举都不可以,都会直接让导航条变透明
3 >使用UIBarMetricsDefault会导致导航控制器的根控制器view的frame(尺寸)包含上面的导航条尺寸
4 >使用UIBarMetricsCompact等等,导航控制器的根控制器view的frame(尺寸),跟屏幕一样大
2. appearance,获取项目中所有控件的外观(标志)
注意: 所有的控件都继承自UIView,UIView遵守UIAppearance协议,所以所有控件都可以使用appearance获取整个应用程序所有外观
appearance 此处代表获取项目中所有导航条
1 >获取整个项目中的外观 appearance
获取整个项目中所有的导航条 appearance
[UINavigationBar appearance]
2 >获取当前类下的所有外观 appearanceWhenContainedIn: self : nil
self 指向RHPNavigationController,获取整个RHPNavigationController类的所有导航条
[UINavigationBar appearanceWhenContainedIn: self , nil ];
3. 类的加载和初始化load方法和initialize方法区别
load
加载类的时候调用
程序一启动就会把所有的类加载进内存,但不会初始化
只调用一次
initialize
第一次使用这个类或它的子类的时候调用
初始化类的时候调用
只调用一次
viewDidLoad
控制器的view加载完成的时候调用
有多少个子控制器就会调用多少次viewDidLoad,耗性能
4. UISegmentedControl 用法
initWithItems:传一个图片数组,或者字符串数组
UISegmentedControl *segement=[[UISegmentedControl alloc] initWithItems: @[ @"足球" , @"篮球" ] ];
设置segment的默认和选中图片
[segement setBackgroundImage:[UIImage imageNamed: @"CPArenaSegmentBG" ] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segement setBackgroundImage:[UIImage imageNamed: @"CPArenaSegmentSelectedBG" ] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
设置segement的主题颜色(相当于设置默认的标题颜色)
[segement setTintColor:RHPColor( 18 , 141 , 141 )];
设置segement的选中颜色为白色
[segement setTitleTextAttributes: @{ NSForegroundColorAttributeName:[UIColor whiteColor] } forState:UIControlStateSelected];
默认选中第一个
segement.selectedSegmentIndex= 0 ;
设置segement的宽高
segement.bounds=CGRectMake( 0 , 0 , 150 , 35 );
5. cell即将显示的时候调用 设置cell参差不齐的出现并携带动画效果
//记录每一次加载cell的平移量
static int _offsetX= 50 ;
//cell即将显示的时候调用
- ( void )tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell的内容向右平移
cell.contentView.transform =CGAffineTransformMakeTranslation(_offsetX, 0 );
[UIView animateWithDuration: 0.5 animations:^{
//取消平移(CGAffineTransformIdentity 设置平移量为0)
cell.contentView.transform =CGAffineTransformIdentity;
}];
_offsetX+= 50 ;
}
6. 在分类中使用@property不会自动生成属性,只会生产get和set方法
在分类中使用 @property 不会自动生成属性,只会生产get和set方法
@property (nonatomic,assign) CGFloat x;
7. 调整按钮内部子控件的方式:
方式一:
// 返回按钮内部UIImageView的尺寸
- (CGRect)imageRectForContentRect:(CGRect)contentRect
// 返回按钮内部UILabel的尺寸
- (CGRect)titleRectForContentRect:(CGRect)contentRect
方式二:
跳转内部子控件的位置
layoutSubviews
8. KVC使用的注意事项
使用KVC字典转模型,字典里面的Key和模型属性名必须一一对应,否则无法使用KVC
9. 保存代码的三种方式
//函数保存一段代码
void test1()
{
[MBProgressHUD showError: @"关于" ];
}
//方法保存一段代码
- ( void ) test2
{
[MBProgressHUD showError: @"关于" ];
}
//block保存代码
void (^block)() = ^() {
[MBProgressHUD showError: @"关于" ];
};
10. 拉伸image
拉伸图片 (向右面拉伸)
UIImage *image=[UIImage imageNamed: @"NLArenaNavBar64" ];
image=[image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5 ];
11. 拦截导航控制器的push操作
//重写push方法
-( void )pushViewController:(UIViewController *)viewController animated:( BOOL )animated{}
12. 拉伸按钮的背景图片
注意: 在storyboard中只能设置UIImageView图片的拉伸,不能设置按钮图片的拉伸,拉伸按钮图片只能通过代码
注意: 如果storyboard中设置了属性,在控件加载完成的时候,属性已经赋值上去了,可以直接获取.
- ( void )viewDidLoad {
[ super viewDidLoad];
//拿到按钮的背景图片
UIImage *image=_loginButton.currentBackgroundImage;
//拉伸背景图片
image=[image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5 ];
//重新给按钮的背景图片赋值
[_loginButton setBackgroundImage:image forState:UIControlStateNormal];
}
13. 使用webView展示网页,并使用代理执行javascript脚本
- ( void )viewDidLoad {
[ super viewDidLoad];
self .title=_html.title;
//添加一个webView,展示网页
UIWebView *webView=[[UIWebView alloc] initWithFrame: self .view.bounds];
[ self .view addSubview:webView];
//展示网页
/**
* 加载网页
* @param URL资源全路径
* @param URLForResource 加载网页的名称
* @param withExtension 后缀名
*/
NSURL *URL=[[NSBundle mainBundle] URLForResource:_html.html withExtension: nil ];
//创建需求
NSURLRequest *request=[NSURLRequest requestWithURL:URL];
//加载请求
[webView loadRequest:request];
// 设置webView的代理,遵守<UIWebViewDelegate>协议,见下方webView代理方法
webView.delegate= self ;
}
#pragma mark webView代理方法
/*
* webview加载完成的时候调用
* 一般执行javascript代码,都要在webview把网页加载完成的时候才执行,因为只有完全加载才知道对应网页的内容
*
*/
-( void )webViewDidFinishLoad:(UIWebView *)webView
{
NSString *javascript=[NSString stringWithFormat: @"window.location.href='#%@'" ,_html.ID];
//webView 执行javascript脚本
[webView stringByEvaluatingJavaScriptFromString:javascript];
}
14. blocks使用
block 作用
1>block 通常是用来保存一段代码,因为有些代码,不一定需要马上调用,而是在恰当的时候采取调用
2>(void(^)() block可以作为方法的参数 保存一段代码,用来执行一段代码
使用block类型作为成员变量的注意事项
注意: block类型的成员变量(ARC可以用Copy或strong,MRC只能用copy)
创建block类型作为成员变量
1>@property (nonatomic,strong) void(^ RHPBlock)() ;
2>typedef void(^RHPBlock)();—>void(^)()的别名,RHPBlock是block名字
block原理
block里面的所有强引用变量都会被强引用一次
block循环引用
1>造成循环引用的原因:如果block代码块里面的变量再次用strong修饰,就会造成循环引用
2>解决block循环引用:一般通过用__weak声明一个弱指针修饰该变量,使其不再强引用
one. 声明一个弱指针的self,使weakSelf不再强引用
__weak RHPViewController *weakSelf=self;
two. 自动识别self类型,并使用弱指针指向,使weakSelf不再强引用, 有逼格,常见抽成宏
__weak typeof(self) weakSelf=self;
three:宏
#define weakSelf(ws) __weak typeof(self) ws=self
//使用
weakSelf(ws);
//解决循环引用
RHPBlock =^(){
UIView *view=[ ws. view];
};