日常小知识点UITableView

iOS小技巧

2018-08-13  本文已影响121人  00d1ed2b53ae

1.卡片式UITableViewCell的处理方式


- (void)setFrame:(CGRect)frame
{
    static CGFloat margin = 10; // margin为离边界距离,可根据需要设定
    frame.origin.x = margin;
    frame.origin.y += margin;
    frame.size.width -= 2 * margin;
    frame.size.height -= margin;
    [super setFrame:frame];
}

2.UIButton之上面为图片,下面为标题的处理方式


#import "SJButton.h"
@implementation SJButton

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 调整图片(图片为正方形)
    CGRect imageRect = self.imageView.frame;
    imageRect.origin.x = 0;
    imageRect.origin.y = 0;
    imageRect.size.width = self.bounds.size.width;
    imageRect.size.height = imageRect.size.width;
    self.imageView.frame = imageRect;

    // 调整文字(文字为长方形,宽度和图片一样,高度为整个Button高度减去图片高度)
    CGRect titleRect = self.titleLabel.frame;
    titleRect.origin.x = 0;
    titleRect.origin.y = self.imageView.bounds.size.height;
    titleRect.size.width = self.imageView.bounds.size.width;
    titleRect.size.height = self.bounds.size.height - titleRect.size.width;
    self.titleLabel.frame = titleRect;

}
@end

在控制器中的初始化代码

SJButton *btn = [SJButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 70, 90);
btn.backgroundColor = [UIColor blackColor];
[btn setImage:[UIImage imageNamed:@"login_sina_icon_click"] forState:UIControlStateNormal];
[btn setTitle:@"微博登录" forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:btn];

3.状态栏的相关设置


- (UIStatusBarStyle)preferredStatusBarStyle // 设置状态栏信息颜色
{
    return UIStatusBarStyleDefault; // 黑字
//    return UIStatusBarStyleLightContent; // 白字
}
- (BOOL)prefersStatusBarHidden // 设置状态栏的Hidden
{
    return YES;
}

4.程序的锁屏设置


[UIApplication sharedApplication].idleTimerDisabled = YES;
或[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

5.CocoaPods pod install/update更新慢的处理


pod install --verbose --no-repo-update 
pod update --verbose --no-repo-update
如果不加后面的参数,默认会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少

6.设置UITableView上accessoryType颜色


self.tableView.tintColor = [UIColor redColor];

7.像safari一样滑动的时候隐藏navigationbar


  self.navigationController.hidesBarsOnSwipe = YES;

8.学会navigationItem的rightBarButtonItems属性


self.navigationItem.rightBarButtonItems = @[
    [UIBarButtonItem itemWithImage:@"mine-setting-icon" highlightImage:@"mine-setting-icon-click" targer:self action:@selector(settingClick)],
    [UIBarButtonItem itemWithImage:@"mine-moon-icon" highlightImage:@"mine-moon-icon-click" targer:self action:@selector(moonClick)]
                                               ];

上一篇 下一篇

猜你喜欢

热点阅读