0

我有一个应用程序,我在其中使用UIButtonatitleUIImage。他们alignment似乎是第一个标题,然后是UIImage.

我试过这个:

[dropdownbutton setImage: [UIImage imageNamed:@"down_sml_arrow.png"] 
                                     forState:UIControlStateNormal];
CGFloat spacing = 10; // the amount of spacing to appear between image and title
dropdownbutton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
dropdownbutton.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
[dropdownbutton setTitle:@"Your text" forState:UIControlStateNormal];

但这里UIImage是在标题之前。谁能指出我哪里出错了?

4

2 回答 2

1

备用

UIImageView *imageView1=[[UIImageView alloc]init];
UIButton *dropdownbutton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
dropdownbutton.frame=CGRectMake(50, 50, 120, 50);
imageView1.image=[UIImage imageNamed:@"arrows.png"];
imageView1.frame=CGRectMake(85, 12, 25, 25);
dropdownbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[dropdownbutton addSubview:imageView1];
CGFloat spacing = 10; // the amount of spacing to appear between image and title
dropdownbutton.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
[dropdownbutton setTitle:@"Your text" forState:UIControlStateNormal];
[self.view addSubview:dropdownbutton];
于 2013-06-20T11:58:05.337 回答
0

Sometimes creating a subclass of IUButton makes things easier, because the aligntments of the image and text are dependant of each other. Create a subclass of UIButton:

@interface DropDownButton : UIButton 

and override layoutSubviews method in the implementation (.m) file:

@implementation DropDownButton

- (void)layoutSubviews {

[super layoutSubviews];

//Use the lines below if you have a specific image for the button
//float imageWidth = 67; 
//float imageHeight = 25;

UIImageView *imageView = [self imageView];
UILabel *label = [self titleLabel];

CGRect imageFrame = imageView.frame;
CGRect labelFrame = label.frame;

labelFrame.origin.x = (self.frame.size.width * 1) / 3;
labelFrame.size.width = self.frame.size.width;


imageFrame.origin.x = 10;

[label setShadowOffset:CGSizeMake(0, -0.5)];
[label setFont:[UIFont fontWithName:@"Helvetica" size:22.0]];
[label setTextAlignment:UITextAlignmentLeft];

imageView.frame = imageFrame;
label.frame = labelFrame;

}

If you insist on using edge insets try to use

dropdownbutton.contentMode = UIViewContentModeLeft;

before giving the insets; it might help.

于 2013-06-20T11:54:02.817 回答