I created a custom UIButton class and overwrote the init Method :
- (id)initWithFrame:(CGRect)frame title:(NSString *)titulo
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
setaDown = [UIImage imageNamed:@"setaDown"];
separator = [UIImage imageNamed:@"separatorLine"];
self = [UIButton buttonWithType:UIButtonTypeCustom];
[self setFrame:frame];
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
seta = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width-setaDown.size.width*2, frame.size.height/2, setaDown.size.width/1.2, setaDown.size.height/1.2)];
[seta setImage:setaDown];
[self addSubview:seta];
separatorLine = [[UIImageView alloc]initWithFrame:CGRectMake(5, frame.size.height-2, frame.size.width-10, separator.size.height)];
[separatorLine setImage:separator];
[self addSubview:separatorLine];
[self setTitle:titulo forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
The problem is, the images doesnt show in the button, and i have a exc_bad_access
, in the line :
seta = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width-setaDown.size.width*2, frame.size.height/2, setaDown.size.width/1.2, setaDown.size.height/1.2)];
dont know exaclty why im getting this error, and how to solve it.
You can create a custom UIControl
, it has touchUp event
and put any kind of components on it. If the UIButton
doesn't meet your need, then you can make a custom UIControl
.
Ex:
class MyButton: UIControl {
@IBOutlet var myImage: UIImageView!
@IBOutlet var myTitle: UILabel!
@IBOutlet var myImage2: UIImageView!
override var isSelected: Bool {
didSet {
// Configure image for `isSelected` status been changed.
myImage.image = (isSelected ? selImg : normalImg)
}
}
override var isEnabled: Bool {
didSet {
// Configure for `isEnabled` status been changed.
myTitle.isEnabled = isEnabled
}
}
}
A UIButton
already has an imageView
. You can simply use
self.imageView.image = setaDown
and configure other aspects of the imageView
as needed (for example, position and size so that your separator
fits).
As far as I know, you can't add 2 images to a button. Get an image editor, composite them together and then do this:
[self setBackgroundImage:[UIImage imageNamed:@"mynewimagename"] forState:UIControlStateNormal]
Much simpler.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.