我正在尝试根据按下状态为 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);