&iOS

iOS 自定义button 在点击buttom后图片发生偏移问

2018-01-17  本文已影响50人  木木是熊猫

系统UIButton不满足需求 所以需要自定义一个button
.m代码如下



- (void)awakeFromNib {
    
    [super awakeFromNib];
    
}


- (void)layoutSubviews
{


        [super layoutSubviews];
    
        //设置imageview
        self.imageView.y = 0;
        self.imageView.centerX = self.width * 0.5;
    
        self.imageView.width = 66*FITNUM;
        self.imageView.height = 66*FITNUM;
    
        //设置label
        [self.titleLabel sizeToFit];
        self.titleLabel.y = self.height - self.titleLabel.height;
        self.titleLabel.centerX = self.width * 0.5;

    
}

本以为大功告成 小意思
实际运行中问题来了!


084B33C4FA631B09C767666EA02530A2.png

左边图为点击前的样子 右边是点击按钮后 可以明显看到点击后图片的位置发生了变化

解决方法其实很简单

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
}


- (void)layoutSubviews
{
    
    
    [super layoutSubviews];
    
    
    self.imageView.width = 66*FITNUM;
    self.imageView.height = 66*FITNUM;
    //设置imageview
    self.imageView.y = 0;
    self.imageView.centerX = self.width * 0.5;
    
    
    
    //设置label
    [self.titleLabel sizeToFit];
    self.titleLabel.y = self.height - self.titleLabel.height;
    self.titleLabel.centerX = self.width * 0.5;
    
    
}

把给imageView赋值放到设置位置约束之前就好了 (虽然是个小问题 但是因为耗费了少许时间 所以特意记载下来)

上一篇下一篇

猜你喜欢

热点阅读