UIButton titleLabel在新闻发布会上调整大小?

时间:2012-08-08 22:12:20

标签: ios uikit

我试图根据按下的状态给出一个UIButton(用resizableImages创建)一些文本。我所看到的行为是当我按下按钮图像时,按钮图像的宽度会变化(变宽)。我想要的是当我按下它时文本改变,但是按钮保持相同的宽度。我已经摆弄了许多UIButton选项,并且没有任何运气。以下是我目前正在做的事情:(有趣的是,按钮宽度变化的数量似乎与按钮框架的宽度相关(更大 - >更多更改):

UIImage *normalButton = [UIImage imageNamed:@"ButtonSlice.png"];
UIImage *pressedButton = [UIImage imageNamed:@"ButtonPressedSlice.png"];
normalButton = [normalButton resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2)];
pressedButton = [pressedButton resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2)];

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
[myButton setImage:normalButton forState:UIControlStateNormal];
[myButton setImage:pressedButton forState:UIControlStateHighlighted];
[myButton addTarget:self action:@selector(pressed) forControlEvents:UIControlEventTouchUpInside];

myButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:18.0];

[myButton setTitleColor:[UIColor colorWithWhite:0.1 alpha:1] forState:UIControlStateNormal];
[myButton setTitle:@"Highlighted" forState:UIControlStateHighlighted];
[myButton setTitle:@"Default" forState:UIControlStateNormal];
myButton.titleLabel.textAlignment = UITextAlignmentCenter;

//No effect?
myButton.adjustsImageWhenHighlighted = NO;

myButton.frame = CGRectMake(ceil(controllerFrame.size.width/2 - 100), 80, 200, 50);

0 个答案:

没有答案