美文网首页iOS开发进阶程序员Unity技术分享
iOS常见小知识总结(持续更新中)

iOS常见小知识总结(持续更新中)

作者: 安静守护你 | 来源:发表于2018-03-28 23:38 被阅读6次

1. UITableView某一行分割线移除问题

①. 整个tableview的分割线都隐藏,在cell中自定义
self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
②. 隐藏某一行的分割线
[cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, MAXFLOAT)];

2. UITableView某一行分割线与cell等宽问题

cell.preservesSuperviewLayoutMargins = false;
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;

3. 让navigationItemUIBarButtonItem的背景色不为蓝色,显示图片本身颜色

UIImage *image = [[UIImage imageNamed:@"icon-xingji-pr"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:(UIBarButtonItemStylePlain) target:self action:@selector(clickMessageAction:)];

在图片初始化的时候,设置图片的渲染方式为UIImageRenderingModeAlwaysOriginal即可

4. 在阅读别人的代码时,突然看到autocorrectionType这个属性,以前也没有使用过,不知道是干嘛用的,经过查询资料才发现,此属性是用来设置是否使用苹果自动更正功能(仅限于UITextViewTextFieldSearchBar这些输入框的)

属性 介绍
autocorrectionType 定义文本是否使用苹果自动更正功能 UITextAutocorrectionTypeDefault 默认
UITextAutocorrectionTypeNo
UITextAutocorrectionTypeYes
autocapitalizationType 定义文本自动大小写样式 UITextAutocapitalizationTypeNone
UITextAutocapitalizationTypeWords
UITextAutocapitalizationTypeSentences 默认
UITextAutocapitalizationTypeAllCharacters
spellCheckingType 定义文本拼写检查样式 UITextSpellCheckingTypeDefault 默认
UITextSpellCheckingTypeNo
UITextSpellCheckingTypeYes
alwaysBounceVertical 定义textview或者scrollview在垂直方向上拥有弹簧效果 YES
NO 默认

5.修改UITextFieldplaceholder文字的颜色

A:通过KVC来修改
self.inviteCodeTF.placeholder = @"请输入邀请码";
[self.inviteCodeTF setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
B:UITextFieldattributedPlaceholder这么一个属性,接下来的事情就好办咯
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:
    @{NSForegroundColorAttributeName:[UIColor redColor],
                 NSFontAttributeName:textField.font
         }];
    self.phoneTF.attributedPlaceholder = attrString;

6. 修改APP状态栏的颜色

app状态栏的颜色是个枚举值,有两种状态,一种是默认的黑色UIStatusBarStyleDefault,另外一种就是白色UIStatusBarStyleLightContent
修改的方法如下:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
/// 然后在plist文件中添加`View controller-based status bar appearance`的值为`NO`即可

7. iOS11UITableView偏移 20/64 适配

在开发的某个页面中,导航栏隐藏self.navigationController.navigationBarHidden = YES;,然后声明的UITableViewheaderView在iOS11上无缘无故的就偏移了20;iOS11以下没问题
解决方案:

    if (@available(iOS 11.0, *)) {
        self.tableV.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

8. iOS复制字符串到粘贴板中

项目需求是这个样纸滴:有一个邀请码,点击复制按钮要能够将邀请码复制到粘贴板中。
这就需要使用到iOS中的UIPasteboard了,这个类中提供了很多方法,具体的可以点进去查看。

    // 粘贴代码
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = @"AAABBB";
    // 自行根据需求进行提示复制成功即可

9. iOS判断当前设备是否为iPhone X

这种判断直接搞个宏定义就OK,也方便调用

#define kIsiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO) 

10. iOSiPhone X适配

参考文章:iPhone X 适配(全)

11. 使用AFN3.0获取HTTP请求的状态码

NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSInteger stateCode = response.statusCode;  // 获取返回状态码
NSDictionary *headers = response.allHeaderFields;  // 获取头部信息

相关文章

网友评论

    本文标题:iOS常见小知识总结(持续更新中)

    本文链接:https://www.haomeiwen.com/subject/wlcpcftx.html