iOS开发中的一些小问题

自己在开发过程中或者交流中发现的一些小问题,收集起来以后自己来看。

1,当scrollView(包括它的子视图tableView,collectionView等)作为控制器的self.view的第一个子视图的时候,它会默认向下移动64的距离,那么如何禁止它的这种默认设定呢?

//注意,self是控制器
self.automaticallyAdjustsScrollViewInsets = NO;

2,当scrollView的内容小于他的尺寸的时候,如何让它具有同样具备弹簧的效果呢?

  //垂直方向
    self.collectionView.alwaysBounceVertical = YES;
    //水平方向
    self.collectionView.alwaysBounceHorizontal = YES;

tableView默认是打开的,而collectionView默认是关闭的

3,设置了约束的时候,UIScrolleView的contentOfSize 是无效的
当你设置了约束的时候,你的滑动视图的尺寸只会取决于你的约束,而即使你设置了contentofSize也是无效的,假如你的约束没有明确的把宽高告诉它,那它可能就没有尺寸哦,应该有童鞋遇到过这个问题吧

4,如何将self和全局变量设置为弱引用:

        __weak __typeof__(self) weakSelf = self;

这个大家都知道,但是如果在.m文件中有一个全局变量 比如 _currentPage 呢,如果在block中直接使用 _currentPage 同样会造成强引用的,如何将_currentPage也变成弱引用呢?

__weak __typeof __(self) weakself = self;
__strong __typeof __(self) strongself = weakself;
strongself->_currentPage = 2;

5,你知道如何为类目加上属性吗?
Objective-C特性:Runtime

Method Swizzling

6,当UIButton既有图片又有文字的时候,你知道如何调整他们的位置吗?

[UIButton new].titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[UIButton new].imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

7,如何设置视图最上方的状态栏的颜色呢?
无法直接设置状态栏的颜色的,因此我们可以给最上方的高20的地方加个view:

 let statusBarView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.hc_width, height: 20))
statusBarView.backgroundColor=Common.gd_tabBarTinColor;
self.view.addSubview(statusBarView)

用 swift 写的,Object-C原理一致。

8,如果你想得到一个控制器的navigationItem,那你应该用:self.navigationItem 而不要用:self.navigationController.navigationItem,但是你要用navigationBar的时候,你要用:self.navigationController.navigationBar

9,如何判断是否为第一次启动?

 if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
//        NSLog(@"第一次启动");


    }else{
        NSLog(@"不是第一次启动");
    }

10,如何给导航栏,状态栏设置统一风格呢?

  [[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance]setBarTintColor:kStatusBarColor];

11,为什么你的右滑返回会失效呢?如何解决右滑失效的问题?
当你自定义了leftBarButtonItem 或者leftBarButtonItems的时候,右滑返回会失效
解决办法1:不要自定义leftBarButtonItem,而使用backBarButtonItem,如果你觉得按钮的位置需要调整的话,可以见问题6的解决办法
解决办法2:

self.navigationController.interactivePopGestureRecognizer.delegate =(id<UIGestureRecognizerDelegate>)self;

仅仅这么做,会遇到一个Bug,当你的转场动画还没完立即右滑返回,会崩溃。所以你需要在push的过程中,禁止滑动手势,然后在push完成之后打开滑动手势,

@interface BaseNavigationController : UINavigationController <UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@end

@implementation BaseNavigationController

- (void)viewDidLoad
{
  __weak BaseNavigationController *weakSelf = self;

  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
  {
    self.interactivePopGestureRecognizer.delegate = weakSelf;
    self.delegate = weakSelf;
  }
}

// Hijack the push method to disable the gesture

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    self.interactivePopGestureRecognizer.enabled = NO;

  [super pushViewController:viewController animated:animated];
}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
  // Enable the gesture again once the new controller is shown

  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    self.interactivePopGestureRecognizer.enabled = YES;
}
@end

参考链接:
ios7 在自定义leftBarButtonItem情况下的右滑返回问题

解决办法3:
self.navigationController.interactivePopGestureRecognizer.delegate =(id<UIGestureRecognizerDelegate>)self;
这句代码写在viewWillAppear中。不清楚能不能用,在别人的博客中看到了这种做法于是就加上了。慎用。

12,你知道如何拉伸图片而保证图片不变形吗?

UIImage *image = [[UIImage imageNamed:@"xxx"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
  [[UIImage imageNamed:@"xxx"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 30, 50)];

上一个已经弃用了,下一个的原理如下:
这里写图片描述
像这张图我可以只放大星空的范围,而不会放大人物的尺寸了。

13,离屏渲染会影响性能,例如给view加阴影,加圆角都属于离屏渲染的一种,一两个还没什么关系,如果很多就会严重影响页面的push时间,最好的办法是用图片代替。

14,点击按钮的时候,如果让它具备像点击图片那样的效果?例如这样:
1,导入 UIImage+YYAdd.h
2,代码:

[exitloginBtn setBackgroundImage:[UIImage imageWithColor:ColorFromRGB(0x3a87ec)] forState:UIControlStateNormal];

使用 imageWithColor就可以达到效果。

15,再tableview中,在didSelect中弹出alertView时,发现弹出的特别慢?,我觉得可能是主线程被阻塞的原因,后面找到了两个解决办法:
1,uitableViewCell的selectionStyle设置为UITableViewCellSelectionStyleDefault,基本能够满足要求,
2,将alertview的弹出放在GCD中,代码:

dispatch_async(dispatch_get_main_queue(), ^{
           //适配iOS9以下
           if (NSClassFromString(@"UIAlertController")) {
               NSString  * str = [NSString stringWithFormat:@"请选择%@",[titleArray[indexPath.section] allKeys][0]];
               UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"选择" message:str preferredStyle:UIAlertControllerStyleActionSheet];
               for (int i = 0; i < array.count; i++) {
                   UIAlertAction * ac = [UIAlertAction actionWithTitle:array[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                       if (indexPath.section == 0) {
                           if (i == 0) {
                               self.type = XAOrderTypeDaDan;
                           }else{
                               self.type = XAOrderTypeSanDan;
                           }
                       }
                       //更新数据
                       [self.data setValue:array[i] forKey:[self getPropertyName:[titleArray[indexPath.section] allKeys][0]]];
                       cell.rightTitle = [self.data valueForKey:[self getPropertyName:[titleArray[indexPath.section] allKeys][0]]];

                   }];
                   [alertVC addAction:ac];
               }
               UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
               }];
               [alertVC addAction:cancel];
               [self presentViewController: alertVC animated: YES completion: nil];
           }else{
               __weak __typeof(self)weakSelf = self;
               NSString  * str = [NSString stringWithFormat:@"请选择%@",[titleArray[indexPath.section] allKeys][0]];
               UIActionSheet * as = [[UIActionSheet alloc]initWithTitle:str delegate:weakSelf cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
               for (int i = 0; i < array.count; i++) {
                   [as addButtonWithTitle:array[i]];
               }
               [as showInView:weakSelf.view];
           }

第二个方法基本可以完美解决这个问题

16,如何让一个普通的UIViewController中的内容具备滑动效果呢?,总体的思路就是用一个比self.view的稍微大一点点的scrollView去替换掉原本的self.view,
代码:

- (void)loadView {
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.delaysContentTouches = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    self.view = scrollView;

    UIView *contentView = [UIView new];
    [scrollView addSubview:contentView];

    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(scrollView);
        make.width.mas_equalTo(@(kScreenWidth));
        make.height.greaterThanOrEqualTo(scrollView).offset(1).priorityHigh();
        //        make.bottom.equalTo(scrollView).priorityLow();
    }];
}
- (void)viewDidLayoutSubviews {
    CGSize contentSize = ((UIScrollView *)(self.view)).contentSize;
    CGFloat minScrollHeight = self.view.frame.size.height + 1 - ((UIScrollView *)(self.view)).contentInset.top - ((UIScrollView *)(self.view)).contentInset.bottom;
    if (contentSize.height < minScrollHeight) {
        contentSize.height = minScrollHeight;
        ((UIScrollView *)(self.view)).contentSize = CGSizeMake(kScreenWidth, minScrollHeight);
    }
}

这个可以作为一个基类,在有需要的地方用到就可以

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值