title: iOS小知识点集锦
date: 2017-06-01 15:28:57
categories: "iOS"
tags:
- Objective-C
description: 自己记录的小知识点
- UIView 视频全屏 masonry 约束设置
UIView *player = [[UIView alloc] init];
[self.view addSubview:player];
CGFloat wid = [UIScreen mainScreen].bounds.size.width;
CGFloat hei = [UIScreen mainScreen].bounds.size.height;
CGFloat rate = wid < hei ? (wid/hei) : (hei/wid);
[player mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view);
make.left.right.equalTo(self.view);
make.height.mas_equalTo(player.mas_width).multipliedBy(rate);
}];
- Masonry实现动画更新约束
// 告诉self.view约束需要更新
[self.view setNeedsUpdateConstraints];
// 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
[self.view updateConstraintsIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];
- UISlider更改进度条的粗细 、修改圆点位置
//需要重写UISlider的trackRectForBounds 方法
- (CGRect)trackRectForBounds:(CGRect)bounds {
bounds.origin.y = bounds.size.height / 2.0 - 2;
bounds.size.height = 4;
return bounds;
}
//小圆点Rect修改
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
{
rect.origin.x = rect.origin.x - 3 ;
rect.size.width = rect.size.width +6;
return [super thumbRectForBounds:bounds trackRect:rect value:value];
}
- iOS获取View所在的控制器、导航控制器
//获取控制器
- (UIViewController*)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UINavigationController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
}
//获取导航控制器
- (UINavigationController*)navigationController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UINavigationController class]]) {
return (UINavigationController*)nextResponder;
}
}
return nil;
}
- UILabel 设置行间距
- (NSAttributedString *)getParagraphText:(NSString*)text LineSpacing:(CGFloat)spacing{
if(text == nil || text.length == 0){
return nil;
}
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:spacing];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
return attributedString;
}
//使用
self.titleLabel.attributedText = [self getParagraphText:infModel.title LineSpacing:7];
- UIView获取父控制器/父导航控制器
//创建UIView的分类
//获取当前控制器
- (UIViewController *)getCurrentVC {
UIResponder *next = self.nextResponder;
do {
//判断响应者是否为视图控制器
if ([next isKindOfClass:[UIViewController class]]) {
return (UIViewController *)next;
}
next = next.nextResponder;
} while (next != nil);
return nil;
}
//获取导航控制器
- (UINavigationController *)getCurrentNavVC {
UIResponder *next = self.nextResponder;
do {
//判断响应者是否为视图控制器
if ([next isKindOfClass:[UINavigationController class]]) {
return (UINavigationController *)next;
}
next = next.nextResponder;
} while (next != nil);
return nil;
}
- 拉伸图片
//拉伸图片
+ (UIImage *)resizedImageWithName:(NSString *)name
{
return [self resizedImageWithName:name left:0.5 top:0.5];
}
+ (UIImage *)resizedImageWithName:(NSString *)name left:(CGFloat)left top:(CGFloat)top
{
UIImage *image = [UIImage imageNamed:name];
return [image stretchableImageWithLeftCapWidth:image.size.width * left topCapHeight:image.size.height * top];
}
- **判断当前控制器是push 还是present **
//方法一
- (IBAction)dismiss:(id)sender {
if (self.presentingViewController) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
//方法二
- (IBAction)dismiss:(id)sender {
if (self.navigationController.topViewController == self) {
[self.navigationController popViewControllerAnimated:YES];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
原文地址: http://oxy.pub