整理项目中遇到的小技巧

1、在UITextField中输入内容时,内容向下偏移

解决:UITextFiled在非编辑状态只有一个子类UITextFiledLabel,在编辑状态时,UITextField的子类视图发生了变化,有一个子类视图时UITextFieldEditor,这个视图继承自UIScrollView 。 参考资料设置控制器的属型automaticallyAdjustsScrollViewInsets = NO。

2、创建xib时,从xib拖线生成对应的属性。在确定xib和对应的类进行关联之后,拖线时无法生成对应的属性。(Xcode有时就是不好使)

解决:在对应类的@interface @end 里手写IBOutlet属性,反向拖线就好了
控制器拖线.gif

3、在有导航栏的情况下设置状态栏(statusBar)为lightContent时,失去效果。

解决: self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
状态栏的颜色会受到导航栏的样式的影响。

4、正确设置导航栏的标题和标签栏标题的正确姿势

解决:  
  childController.navigationItem.title ; 设置导航栏的标题
  childController.tabBarItem.title;设置标签栏的标题
  childControllers.navigationController.tabBarItem.title;有导航栏控制器时设置标签栏的标题

5、修改tableViewCell的accessoryView的样色样式(tintColor功能很强大,可以设置一些barTintColor,tintColor试一试第三条对状态栏,导航栏的影响)

解决:tableView.tintColor = [UIColor redColor];

6、设置tableViewCell的分割线满屏显示

解决: tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

7、使用xib设置tableView的headerView的时候,在显示的时候headerView显示不完整

解决: CLDetailHeaderView *headerView = [CLDetailHeaderView detailHeaderView];
  // 计算headerView的大小,设置好frame之后赋值给tableHeaderView才可以计算出合理大小的tableHeaderView
  UIView *header = [[UIView alloc]initWithFrame:headerView.bounds];
  [header addSubview:headerView];
  
  headerView.foodModel = _foodModel;
  self.tableView.tableHeaderView = header;```

#8、不显示没有内容的tableViewCell

解答: // 8、取消多余的cell
self.tableView.tableFooterView = [[UIView alloc] init];

#9、在tableView或者collectionView浏览页面的时候,需要回到顶部的时候,在页面上加上一个回到顶部的按钮

解答: [self.tableView setContentOffset:CGPointZero animated:YES];

#10、给系统的readonly属性进行修改,一般会考虑使用KVC。比如自定义标签栏

CLTabBar *myTabBar = [[CLTabBar alloc] init];
[self setValue:myTabBar forKey:@"tabBar"]; // 给系统的tabBar属性赋值进行替换

  • (void)layoutSubviews{
    [super layoutSubviews];
    // 给tabBar的所有的tabBarButton添加点击事件,然后在点击事件中实现tabBarItem的动画效果
    for (UIControl *tabBarButton in self.subviews) {
    if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {

          [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
      }
    

    }
    }

  • (void)tabBarButtonClick:(UIControl *)tabBarButton
    {
    for (UIView *imageView in tabBarButton.subviews) {
    NSLog(@"%@",tabBarButton.subviews); // 查找需要设置动画的对象
    if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
    //帧动画
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    // 设置动画类型为缩放动画
    animation.keyPath = @"transform.scale";
    animation.values = @[@1.0,@1.02,@0.9,@1.02,@0.90,@1.02,@1.0];
    animation.duration = .8;
    // 动画的进行线性变化
    animation.calculationMode = kCAAnimationCubic;
    //把动画添加到需要动画的tabBarItem的imageView的layer上
    [imageView.layer addAnimation:animation forKey:nil];
    }
    }
    }

#11、按钮只有图片的时候,设置按钮大小和图片等大

解决:button.bounds.size = button.currentBackgroundImage.size ;

#12、给一个视图添加背景图片

解决:1、view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]]; // 纯色图片以平铺的方式设置view的背景
2、view.layer.contents = (__bridge id)([UIImage imageNamed:@"bg"].CGImage);// 设置view的layer的内容

#13、在自定义tabBar时用UIButton替换系统的UITabBarButton时,需要UIButton在点击的时候不要出现闪亮的情况

解决:重写一下UIButton的setHighlighted方法
// 去掉按钮的高亮状态

  • (void)setHighlighted:(BOOL)highlighted{
    在这个方法里面不要调用 super 否则写了相当于没写
    }
#14、保存按钮的选中状态,需要添加一个按钮属性。@property (nonatomic,strong) UIButton *selectedButton;

解答:

  • (void)selectButton:(UIButton *)sender {
    _selectedButton.selected = NO;
    sender.selected = YES;
    _selectedButton = sender;
    }
#15、在代码编写过程中发现变量名称书写不合理,同时修改多个同名的变量名称

![编辑技巧.gif](http://upload-images.jianshu.io/upload_images/2112071-f14565ed38739460.gif?imageMogr2/auto-orient/strip)

#16、Xcode8注释失效

"这个是因为苹果解决xcode ghost。把插件屏蔽了解决方法

命令运行: sudo /usr/libexec/xpccachectl
然后必须重启电脑后生效 "
或者
(1)查看 Xcode -> Preferences -> KeyBingdings

(2)我们可以找到 structure - AddDocumentation 这个就是添加注释的 按照自己需求可以更改这里需要的快捷键,现在变成了 "Alt + Command + /"

(3)取消// 的注释 的快捷键 也和之前我们常用的 "Command + /" 不一样 如果 不喜欢 仍然可以改回来

(4)改好了退出 Preferences 到这里我的Xcode还是不好使 把电脑重启后 就都重新正常使用了

#17、获取手机电量,增强手机的续航能力,提高用户体验。 苹果 在 iOS 9 中为 iPhone 添加了低电量模式 。在此模式下,系统会通过禁用一些特性诸如电子邮件自动获取、后台刷新、Hey Siri等,来达到降低能耗的目的。当我们检测到用户开启了低电量模式,可以为用户做一些小改变,来帮助用户延长电池续航。一般我们会停止后台下载任务,停止定位服务,停止网络请求等,来减少手机电池的电量消耗问题。我们可以在控制器中监控手机的电量,当手机电量没低于0.05的电量时就会发送通知,我们可以在接收到通知的时候进行相应的操作。

CGFloat battery = [UIDevice currentDevice].batteryLevel;// 0~1之间
// 在真机上必须要打开允许电池监听不然会认为是模拟器 模拟器电量100%时会是-1
[UIDevice currentDevice].batteryMonitoringEnabled = YES;

#18、设置手机的旋转方向

1、- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES 表示支持所有的方向 下面👇的方法表示不支持向下的方向
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

2、关于屏幕方向的枚举
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, // 竖直方向
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, // 竖直向下
//because rotating the device to the left requires rotating the content to the right. 设备向左旋转 设备显示的内容向右旋转
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,// 横屏向左
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft横屏向右
} UIInterfaceOrientation;

#19、在升级Cocoapods的时候报下面这个错误的解决办法:

![升级错误信息.png](http://upload-images.jianshu.io/upload_images/2112071-58a83fd87730423d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

解决办法:执行下面的终端命令就好了
$ mkdir -p $HOME/Software/ruby
$ export GEM_HOME=$HOME/Software/ruby
$ gem install cocoapods
$ export PATH=$PATH:$HOME/Sofware/ruby/bin
$ pod --version // 查看pod版本号 验证是否升级成功

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容