iOS总结之小技巧

1、如何设置TableView不显示内容的Cell的分割线消失?

试试这行代码:

self.tableView.tableFooterView = [[UIView alloc] init];

2、隐藏某行cell

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 需要隐藏的那一行,返回高度为0
    if(indexPath.row == YouWantToHideRow)
        return 0; 
    return 44;
}

// 然后在需要隐藏cell的时候调用
[self.tableView beginUpdates];
[self.tableView endUpdates];

3、怎么改变UITextField placeholder的颜色和位置?

继承UITextField,重写drawPlaceholderInRect

- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}

4、如何更高效地给UIImageView绘制圆角?

(1)贝塞尔曲线切割圆角

- (UIImageView *)roundedRectImageViewWithCornerRadius:(CGFloat)cornerRadius {
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = bezierPath.CGPath;
    self.layer.mask = layer;

    return self;
}

(2)绘制四个角的遮罩(使用场景受限)

在 UIImageView 上添加一个四个角有内容,其它部分是透明的视图,只对 UIImageView 圆角部分进行遮挡。但要保证被遮挡的部分背景色要与周围背景相同,避免穿帮。所以当 UIImageView 处于一个复杂的背景时,是不适合使用这个方法的。

(3)最不推荐做法(当一个页面只有少量圆角图片时才推荐使用)

UIImageView *imageView = [[UIImageView alloc] init];
imageView.layer.cornerRadius = 5.f;
imageView.layer.masksToBounds = YES;

4、如何给UIButton设置复杂的背景图片?

给UIButton的背景图片,如果是复杂的图片,可以依靠 UI 切图来实现。具体代码:

+ (UIImage *)pureColorImageWithSize:(CGSize)size color:(UIColor *)color cornRadius:(CGFloat)cornRadius {
  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, size.height)];
  view.backgroundColor = color;
  view.layer.cornerRadius = cornerRadius;
  // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数是屏幕密度
  UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
  [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return image;
}

5、

UIImage 有一个属性叫size,当为对象Image赋值一张图片以后,Image会自动生成一个size,那么也就是说你可以用image.size.x 来设置其父视图的size。即:

UIImageView *imageView = [[UIImageView alloc]initWithImage:@"text.png"];  

这样不用给定frame,系统也可以显示出带图片大小的ImageView。

6、去除数组中重复的对象

NSArray *newArr = [oldArr valueForKeyPath:@“@distinctUnionOfObjects.self"];

7、获取当前的UIWindow

+(UIWindow*)getWindow {
    UIWindow* win = nil; //[UIApplication sharedApplication].keyWindow;
    for (id item in [UIApplication sharedApplication].windows) {
        if ([item class] == [UIWindow class]) {
            if (!((UIWindow*)item).hidden) {
                win = item;
                break;
            }
        }
    }
    return win;
}

8、修改UITextField的placeholder的字体颜色、大小

[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];

9、设置UIImage圆角

- (UIImage *)circleImage {
    // NO代表透明
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 1);
    // 获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 添加一个圆
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    // 方形变圆形
    CGContextAddEllipseInRect(ctx, rect);
    // 裁剪
    CGContextClip(ctx);
    // 将图片画上去
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

10、获取某个字体的高度

UIFont *font = [[UIFont alloc] fontWithSize:2];
CGFloat fontHeight = font.lineHeight;;

11、获取图片大小

CGFloat imageWidth = image.size.width;
CGFloat imageHeight = imageWidth * image.scale;

12、滑动的时候自动隐藏navigationbar

navigationController.hidesBarsOnSwipe = Yes;

13、图片缩放不止可以用手势,也可以用ScrollView

self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = self.imageView.image.size;
self.scrollView.delegate = self;
// 设置缩放比例
self.scrollView.maximumZoomScale = 2.0;
self.scrollView.minimumZoomScale = 0.1;
/** 
 * 这个方法的返回值决定了要缩放的内容
 * return value 只能是ScrollView的子控件或孙子控件 
 */
 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
 	return self.imageView;
 }

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
	//可以查看缩放比例
	scrollView.zoomScale;
}

顺便提一句,viewForZoomingInScrollView不仅仅可以缩放图片,只要是继承自UIIView的控件都可以进行缩放。

待更新…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值