UIEdgeInsetsMake的使用

UIEdgeInsets UIEdgeInsetsMake (
   CGFloat top,
   CGFloat left,
   CGFloat bottom,
   CGFloat right
);
他的作用就是定义一个在scrollview被拽出一个contentOffset 的时候的一个空间
配合blocks可以实现下拉刷新中footer部分的停留
例如:
1 [UIView animateWithDuration:0.4 animations:^(void){
    self.tableView.contentInset = UIEdgeInsetsMake([self headerRefreshHeight], 0, 0, 0);
  }];

实现了一个下拉刷新的时候顶部footer的停留。


默认情况下,不设置的效果,都使居中现实,button为150*150

 

使用以下设置后:

  [self setTitleEdgeInsets:UIEdgeInsetsMake( 0.0,-backGroundImag.size.width, 0.0,0.0)];

  [self setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -self.titleLabel.bounds.size.width)];

若要title在图片的上方,则位置相对于图片来说,向上移动-80

    [self setTitleEdgeInsets:UIEdgeInsetsMake( -80.0,-backGroundImag.size.width, 0.0,0.0)];

    [self setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -self.titleLabel.bounds.size.width)];

效果如下:

综上所述,若单独设置一个title或者image在button中的位置,UIEdgeInsets是相对于button的frame来计算的(上,左,下,右,),如果是刚才所描述的情况,则title是相对于image的frame设置的,而image的位置是相对于titel的位置设置的

over!

以上转载自:http://blog.csdn.net/yanxiaoqing/article/details/7230660


 

实例源码:

复制代码
 UIImage * image = [[UIImage imageNamed:@"telIcon"] retain];
    NSLog(@"%@>>",NSStringFromCGSize(image.size));
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:[UIColor yellowColor]];
    [button setFrame:CGRectMake(80, 80, 150, 150)];
    [button setTitle:@"测试位置" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:10.0]];
    
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -image.size.width, 0.0, 0.0)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(15.0, 0.0, 0.0, -button.titleLabel.bounds.size.width)];

    NSLog(@"%@",NSStringFromUIEdgeInsets(button.titleEdgeInsets));
    [button setImage:image forState:UIControlStateNormal];
    
    [self.view addSubview:button];
复制代码


你可能感兴趣的:(IOS)