Vertical center of iOS-Button image and text [center button image and text at the same time], and center ios-Button
In the past, there were not many such requirements, which were often used in recent development. Therefore, we simply encapsulate a Button to vertically center the image and body, and rewrite the layoutSubviews method to achieve this, you can check when the layoutSubviews method is triggered;
-(Instancetype) initWithCoder :( NSCoder *) coder {self = [super initWithCoder: coder]; if (self) {self. layer. shadowColor = [UIColor lightGrayColor]. CGColor; self. layer. shadowOffset = CGSizeMake (0, 1); self. layer. shadowOpacity = 0.1;} return self;} // you can adjust the ratio of this method by yourself-(void) layoutSubviews {[super layoutSubviews]; // calculate the float imgHeight = self. imageView. image. size. height; float imgWidth = self. imageView. image. size. width; // imageView Size self. imageView. frame = CGRectMake (VIEWWIDTH (self)-imgWidth)/2, VIEWHEIGHT (self)/2-imgHeight * 7/7, imgWidth, imgHeight); // titleLabel size [self. titleLabel sizeToFit]; float titleWidth = VIEWWIDTH (self. titleLabel); float titleHeight = VIEWHEIGHT (self. titleLabel); self. titleLabel. frame = CGRectMake (VIEWWIDTH (self)-titleWidth)/2, VIEWHEIGHT (self)/2 + imgHeight/7 + 20 * VIEWHEIGHT (self)/900, titleWidth, titleHeight ); self. titleLabel. textAlignment = NSTextAlignmentCenter ;}