Coding过程中遇到的一些bug

1.

在使用layoutSubviews方法调整自定义view内部的子控件坐标时,最好不要使用子控件的centerX,centerY属性,否则会出现奇怪的bug。

如果一定要用,务必仔细检查,该子控件的width,height是否已经赋值。

eg1. 在self.imageView.width尚未赋值时,使用self.imageView.centerX

/**自定义控件调整内部子控件frame需在该方法中,一旦外面修改自定义控件的宽高frame,或者每次点击按钮,都会立刻调用该方法进行重新布局*/
- (void)layoutSubviews { //必须调父类
    [super layoutSubviews];
    
//self为自定义的Button(80*80),imageView和titleLabel是其内部子控件
    self.imageView.y = self.height * 0.1; 
    self.imageView.centerX = self.width * 0.5; //因为此时还未给self.imageView.width赋值,其宽度为0。这会导致centerX和x的值一样,显示在父控件一半的位置。(即self.imageView.x = 40;)
    self.imageView.height = self.height * 0.5;
    self.imageView.width = self.imageView.height; //此处设置imageView.width为40,这样imageView的frame为(40,10,40,40),此时centerX为60.

//页面加载好了,第一次点击buttion时,会调用layoutSubView方法重新布局,此时imageView已经有宽度,x和centerX各算各的,发现centerX本应该在40的位置,现在却在60位置,于是整个imageView向左移动20点。

eg2. 在self..width尚未赋值时,使用self..centerX 

self.indicatorView.width = button.titleLabel.width; ////此句必须放在centerX上面,否则width未赋值(等于0),centerX和x是一样的

self.indicatorView.centerX = button.centerX; //一定要等于button的中心点,不能等于titleLabel中心点,因为titleLabel坐标是相对于button来算的

2.

[self.tabBar  layoutIfNeeded]; //立刻重新布局,强制刷新,强制布局

[self.view   setNeedsDisplay]; //下次刷新屏幕时(很快,大概1/60秒),重新绘制self.view

- (void)layoutSubviews { }   //该方法不应被手动调用,一般用于重写。它可以用来调整自定义控件内部的子控件frame,一旦外面修改自定义控件的frame/宽高,或者每点击一次按钮,系统会让自定义控件立刻调用该方法进行重新布局*/

 

3.

(1) 所有的UIView都有一个setTranslatesAutoresizingMaskIntoConstraints:BOOL方法,该方法表明是否将Autoresizing设置的参数转换成AutoLayout的constraints约束。因为AutoLayout和Autoresizing是互斥的,所以在使用代码添加AutoLayout约束前必须要禁用Autoresizing(即:将view.translateAutoresizingMaskIntoConstraints = NO)。如果是在Xib或Storyboard中布局,只要勾选了AutoLayout选项,IOS系统会自动帮我们设置该属性为NO;

//UIView.h 任意一个UI控件都有如下属性
@property(nonatomic) BOOL translatesAutoresizingMaskIntoConstraints
 
Description描述:    
A Boolean value that determines whether the view’s autoresizing mask is translated into Auto Layout constraints.

If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to NO, and then provide a nonambiguous, nonconflicting set of constraints for the view.

By default, the property is set to YES for any view you programmatically create(代码创建). If you add views in Interface Builder, the system automatically sets this property to NO.

(2) 

UIView都有一个自动伸缩属性autoresizingMask(位枚举),从Xib加载起来的视图控制器vc,它的vc.view在刚显示时尺寸是(600,600),过一段时间它的尺寸才会自动调整到屏幕大小,在这个过程中Xib中vc.view上放置的子视图因为autoresizingMask默认已设置UIViewAutoresizingFlexibleWidth和UIViewAutoresizingFlexibleHeight,所以子视图的宽和高会随着vc.view尺寸的变化而伸缩,达不到想要的布局。此时解决方法是:设置子控件的autoresizingMask = UIViewAutoresizingNone;即可

 

(3) UIView都有一个autoresizesSubviews属性,决定是否自动调整子控件。默认是YES。该属性等价于storyborad或xib中的Autoresizes Subviews,具体见下图:     

//UIView.h 
@property(nonatomic) BOOL autoresizesSubviews
 
Description用途:    
When set to YES, the receiver adjusts the size of its subviews when its bounds change. The default value is YES.

 

4.谨记:按钮的文字/文字颜色/图片都是分状态的,直接设置是错误的。

//alloc,init出来的system按钮默认黑色,如果改为custom,默认是白色,必须设置颜色

[button  setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//self.titleLabel.textColor = [UIColor blackColor]; //按钮的文字/文字颜色/图片都是分状态的,直接设置是不对的。

 

[button setTitle:square.name forState:UIControlStateNormal];  //正确

//button.titleLabel.text = modal.text;   //错误

button.titleLabel.font = [UIFont systemFontOfSize:16]; //只有字体设置是无需分状态 

5.

 KVO使用时的注意事项:

- (void)viewDidLoad {
    //设置tableFooter(注意:这是给tableView设置footer,上面是给section设置footer)
    self.tableView.tableFooterView = [[WZMeFooterView alloc]init]; //footerView默认高度为0
    //KVO监控footer(KVO必须等被监视的对象创建后,才能开始监控,和通知相反)
    [self.tableView.tableFooterView  addObserver:self forKeyPath:@"height" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}

/**KVO监控并设置tableView的contentSize*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"height"]) { //kvo中不能再对被监控的属性"height"进行++/--操作,否则进入死循环
        
        self.tableView.contentSize = CGSizeMake(0, self.tableView.tableFooterView.y + self.tableView.tableFooterView.height);
    }
}

 6.

Xcode在写代码时,有时某个.m文件会莫名其妙突然没有智能提示了,最可能的原因是在Xcode中写了大篇幅的中文注释/*中文注释*/,尤其是写在@implementation的最前面,就可能出现此问题。可以将该中文注释剪切到其他文件(如.h)中,智能提示马上又回来了。

 7.

@""里面再用到“”,必须进行转义\"xxxx\"

8.

使用数组和字典的禁忌:1.不能添加nil对象 2.当数组或字典中没有元素时,不能再通过array[index]方式访问其中的元素,否则直接崩溃。

9.

  //1.导航控制器的底部工具栏toolbar默认是隐藏的

    self.navigationController.toolbarHidden = NO; //默认为YES不显示

 

  //2.viewController有个hidesBottomBarWhenPushed属性,默认是NO,即:如果给navC设置了toolbarHidden=NO,那么所有push出来的vc默认都会显示底部工具条。这个也合情合理。如果某个vc不想显示底部toolbar,可以设置该属性为YES。

   viewController.hidesBottomBarWhenPushed = YES;   //默认是NO

 

 

 

posted @ 2016-06-15 12:18  stevenwuzheng  阅读(628)  评论(0编辑  收藏  举报