[英]How to add a line below UIButton text like top bar in facebook app.?
这是我为我们的App创建的子类。
头文件:
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface CMTabButton : UIButton
/// The color for the bottom line of the button when it is selected
@property (nonatomic)IBInspectable UIColor *selectedColor;
/// The color for the bottom line of the button when it is not selected
@property (nonatomic)IBInspectable UIColor *defaultColor;
/// The height of the bottom line of the button (default 2 pixels)
@property(nonatomic,assign)IBInspectable CGFloat bottomLineHeight;
@end
实施文件:
#import "CMTabButton.h"
@interface CMTabButton() {
UIView *bottomLine;
NSLayoutConstraint *bottomLineHeightConstraint;
}
@end
@implementation CMTabButton
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)didMoveToWindow {
[super didMoveToWindow];
[self uppdateUI];
}
- (void)setup {
//add the bottom line
[self addBottomLine];
}
- (void)setSelected:(BOOL)selected {
//if the unselected color is transparent, we animate the line in/out by changing the height of the line
if (self.defaultColor==nil) {
bottomLineHeightConstraint.constant = (selected) ? self.bottomLineHeight : 0;
//we force the selected color for the animation so the animation is visible
bottomLine.backgroundColor = self.selectedColor;
[UIView animateWithDuration:0.15
animations:^{
[self layoutIfNeeded];
}
completion:^(BOOL finished) {
if (!selected)
bottomLine.backgroundColor = self.defaultColor;
[super setSelected:selected];
}
];
}
else {
bottomLineHeightConstraint.constant = self.bottomLineHeight;
//else, we animate the color change
[UIView animateWithDuration:0.15
animations:^{
bottomLine.backgroundColor = selected ? self.selectedColor : self.defaultColor;
}
completion:^(BOOL finished) {
[super setSelected:selected];
}
];
}
}
- (void)addBottomLine {
bottomLine = [UIView new];
bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:bottomLine];
bottomLineHeightConstraint = [NSLayoutConstraint constraintWithItem:bottomLine attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
[self addConstraint:bottomLineHeightConstraint];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomLine]|" options:0 metrics:nil views:@{@"bottomLine":bottomLine}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bottomLine]|" options:0 metrics:nil views:@{@"bottomLine":bottomLine}]];
}
- (void)prepareForInterfaceBuilder {
[self uppdateUI];
}
- (void)uppdateUI {
bottomLine.backgroundColor = self.selected ? self.selectedColor : self.defaultColor;
if (self.bottomLineHeight>0) {
bottomLineHeightConstraint.constant = self.bottomLineHeight;
[self layoutIfNeeded];
}
}
@end
要使用它,请在“接口”构建器上选择UIButton,然后将其类名从UIButton更改为CMTabButton,然后可以直接从“接口”构建器设置属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.