iOS开发小知识点汇总

来源:互联网 发布:win10系统优化批处理 编辑:程序博客网 时间:2024/06/07 02:25

1 UITableView

  • 禁止UITableView的滑动
tableView.scrollEnabled = NO;
  • 设置分组显示的每个section的标题
//1.设置代理self.tableView.delegate = self;//2.实现代理方法- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    对每个section的title进行设置.}
  • UITableView的cell中添加得有按钮,怎么在按钮的点击事件中获取到UITableView的cell
// 1.添加按钮的点击监听[cell.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];// 2.在按钮的点击方法中通过button计算出其所在的Cell-(void)buttonClick:(UIButton *)button{    UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];}
  • 设置 cell 间的分割线与左边界无间距
//1.在 controller 的 viewDidLoad 方法中添加如下代码- (void)viewDidLoad {    [super viewDidLoad];    [self.tableView setSeparatorInset:UIEdgeInsetsZero];    [self.tableView setLayoutMargins:UIEdgeInsetsZero];}//2.重写代理方法中的以下方法并添加如下代码- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    [cell setSeparatorInset:UIEdgeInsetsZero];    [cell setLayoutMargins:UIEdgeInsetsZero];}
  • 去掉多余的分割线
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

2 UILabel

  • 设置文字居中显示
label.textAlignment=NSTextAlignmentCenter;
  • 自动调节文字的字体来适应控件宽度
    如果文字字数少,则采用系统的字体或者用户设置的字体来显示文字。
    如果文字字数多,设置这个属性可以使文字的字体大小自适应控件宽度。
label.adjustsFontSizeToFitWidth=YES;

3 UITextField

  • 设置内容的垂直对其方式
 text.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;

其值是一个枚举值,有以下几种值
UIControlContentVerticalAlignmentCenter - 居中对齐
UIControlContentVerticalAlignmentButtom - 底部对齐
UIControlContentVerticalAlignmentTop - 顶部对齐
UIControlContentVerticalAlignmentFill - 填充对齐

  • 改变其光标的颜色
    通过以下的方式可以改变 UITextField 的光标的颜色为无色
textField.tintColor = [UIColor clearColor];
  • 限制UITextField输入的字符数
// 1.设置监听监听UITextField的内容的改变[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];// 2.在textChanged:方法中实现对字符数的限制- (void)textChanged:(UITextField *)textField{    if (textField.text.length > 8) {       textField.text = [textField.text substringToIndex:8];    }}

4 关于时间和日期

  • 获取与今天相隔几天的日期并转换成字符串类型输出
/** * @param date  传入的日期 可以传入[NSDate date] * @param offset 偏移量,即是与传入的日期相差的天数 * @return  转换后的日期的字符串 */+ (NSString *)getDateOfSomeDay:(NSDate *)date offset:(NSInteger)offset{        NSCalendar *identifier= [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];        NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];        [components setDay:([components day] + offset)];        NSDate *mDate= [gregorian dateFromComponents:components];        NSDateFormatter *format= [[NSDateFormatter alloc] init];        [format setDateFormat:@"yyyy-MM-dd"];        return [format stringFromDate:mDate];}

5 NSUserDefaults

  • 删除NSUserDefaults中的内容
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];      [[NSUserDefaults standardUserDefaults] synchronize]; 

6 获取应用的版本号

  • 代码实现获得应用的Verison号:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
  • 代码实现获得build号:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

7 状态栏

更改状态栏的字体颜色为白色,分为两步:

  1. 在项目中找到plist文件,添加View controller-based status bar appearance,设值为NO。
  2. 在APPDelete里面的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里面输入代码
// 设置状态栏为白色    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];

获取状态栏的高度

[UIApplication sharedApplication].statusBarFrame.size.height

8 让APP的启动图片全屏,进入程序后再显示状态栏

  1. 在info.list文件中,加上“Status bar is initially hidden”选项,设值为YES

  2. 在AppDelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加设置[[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

9 清除NSUserDefaults里面的所有数据

 // 先将其转化为字典,然后用forin遍历删除即可        NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];        NSDictionary *dictionary = [defatluts dictionaryRepresentation];        for(NSString *key in [dictionary allKeys]){            [defatluts removeObjectForKey:key];            [defatluts synchronize];        }

10 UIButton

  • 设置按钮的点击跳转
导航栏的按钮的点击跳转到下一个 viewController:[self.navigationController pushViewController:secondVC animated:YES];让当前 viewController 消失:[self.navigationController popViewControllerAnimated:YES];模态的按钮的点击跳转到下一个 viewController:[self presentViewController:secondVC animated:YES completion:nil];让当前 viewController 消失:[self dismissViewControllerAnimated:YES completion:nil]; 

11 获取导航栏的高度

通过以下代码可以获取导航栏的高度

self.navigationController.navigationBar.frame.size.height

12 用UIWebView调用本地文件的方法方法

方法一:NSString *filePath = [[NSBundle mainBundle]pathForResource:@"zhinan" ofType:@"html"]; NSString *htmlStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];[webView loadHTMLString:htmlStr baseURL:[NSURL URLWithString:filePath]];方法二:NSString *filePath = [[NSBundle mainBundle]pathForResource:@"zhinan" ofType:@"html"];NSURL *url = [NSURL fileURLWithPath:filePath]; NSURLRequest *request = [NSURLRequest requestWithURL:url];[webView loadRequest:request];
0 0
原创粉丝点击