• UIView(含所有子视图)分类方法 --- 为了方便设置控件的frame


    部分代码如下:

    //设置控件frame时,先设置尺寸width,height,再设置中心,centerX,centerY
    - (CGFloat)sth_right{ return self.sth_x+ self.sth_width; } - (void)setSth_right:(CGFloat)sth_right{ self.sth_x = sth_right - self.sth_width; } - (CGFloat)sth_bottom{ return self.sth_y+ self.sth_height; } - (void)setSth_bottom:(CGFloat)sth_bottom{ self.sth_y = sth_bottom - self.sth_height; }

     实例分析:

     1.注意一般设置子view位置时,一般使用width或height,及x,y,最好不要使用centerX或Y一般说来不太准确。

     2.分类设置子view位置时一定要重写layoutSubViews方法,并先调用父类方法

    //view分类
    #import <UIKit/UIKit.h>
    @interface UIView (STHViewFrame)
    @property (nonatomic, assign) CGFloat sth_x;
    @property (nonatomic, assign) CGFloat sth_y;
    @property (nonatomic, assign) CGFloat sth_w;
    @property (nonatomic, assign) CGFloat sth_h;
    @property (nonatomic, assign) CGFloat sth_centerX;
    @property (nonatomic, assign) CGFloat sth_centerY;
    @property (nonatomic, assign) CGFloat sth_right;
    @property (nonatomic, assign) CGFloat sth_bottom;
    @end
    
    #import "UIView+STHViewFrame.h"
    
    @implementation UIView (STHViewFrame)
    
    - (CGFloat)sth_x{
        return self.center.x;
    }
    - (void)setSth_x:(CGFloat)sth_x{
        
        CGRect frame  = self.frame;
        frame.origin.x = sth_x;
        self.frame = frame;
    }
    - (CGFloat)sth_y{
        return self.center.y;
    }
    - (void)setSth_y:(CGFloat)sth_y{
        CGRect frame = self.frame;
        frame.origin.y = sth_y;
        self.frame = frame;
    }
    - (CGFloat)sth_w{
        return self.frame.size.width;
    }
    - (void)setSth_w:(CGFloat)sth_w{
        CGRect frame = self.frame;
        frame.size.width = sth_w;
        self.frame = frame;
    }
    - (CGFloat)sth_h{
        return self.frame.size.height;
    }
    - (void)setSth_h:(CGFloat)sth_h{
        CGRect frame = self.frame;
        frame.size.height = sth_h;
        self.frame = frame;
    }
    - (CGFloat)sth_centerX{
        return self.center.x;
    }
    - (void)setSth_centerX:(CGFloat)sth_centerX{
        
        CGPoint center = self.center;
        center.x = sth_centerX;
        self.center = center;
    }
    - (CGFloat)sth_centerY{
        return self.center.y;
    }
    - (void)setSth_centerY:(CGFloat)sth_centerY{
        
        CGPoint center = self.center;
        center.y = sth_centerY;
        self.center = center;
    }
    - (CGFloat)sth_right{
        
        return self.sth_x + self.sth_w;
    }
    - (void)setSth_right:(CGFloat)sth_right{
        self.sth_x = sth_right - self.sth_w;
    }
    - (CGFloat)sth_bottom{
        
        return self.sth_y + self.sth_h;
    }
    - (void)setSth_bottom:(CGFloat)sth_bottom{
        self.sth_y = sth_bottom - self.sth_h;
    }
    @end
    
    //自定义按钮及布局
    #import <UIKit/UIKit.h>
    @interface ViewController : UIViewController
    @end
    @interface customButton : UIButton
    @end
    
    #import "ViewController.h"
    #import "UIView+STHViewFrame.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        NSArray *images = @[@"mine_icon_nearby",@"mine_icon_random",@"mine-icon-activity"];
        NSArray *titles = @[@"位置",@"我的",@"他的"];
        
        for (NSInteger i = 0; i < images.count; i++) {
            
            customButton *button = [[customButton alloc] initWithFrame:CGRectMake(20+i*110, 200, 100, 100)];
            [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
            [button setTitle:titles[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor yellowColor]];
            [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
            [self.view addSubview:button];
        }
    }
    @end
    @implementation customButton
    
    - (void)layoutSubviews{
        [super layoutSubviews];
        NSLog(@"%f%f--%f%f",self.sth_centerY,self.sth_centerX,self.sth_h,self.sth_w);
        self.imageView.sth_centerX = self.sth_w/2;
        self.imageView.sth_y = self.sth_h/2 - 5 - self.imageView.image.size.height/2;
        self.imageView.sth_w = self.imageView.image.size.width;
        self.imageView.sth_h = self.imageView.image.size.height;
        self.titleLabel.sth_y = CGRectGetMaxY(self.imageView.frame)+5;
        self.titleLabel.sth_x = 0;
        self.titleLabel.sth_h = 17;
        self.titleLabel.sth_w = self.sth_w;
    }
    @end
    

      效果图:

    将来的自己,会感谢现在不放弃的自己!
  • 相关阅读:
    接口分类
    HTTPS和HTTP的主要区别
    协议类
    小程序
    Polyfill
    去重数组
    odoo 接口跨域请求报错
    docker-compose 自定义容器名称
    linux ssh 防暴力破解fail2ban
    odoo 知识点
  • 原文地址:https://www.cnblogs.com/TheYouth/p/6516717.html
Copyright © 2020-2023  润新知