好吧,我有一个带有3个UIButtons的自定义uitabbarcontroller(模拟标签)。我为正常,选定和突出显示的状态设置了背景图像(选中和突出显示的都是相同的)。 一切正常,但有一点:当我选择了标签(按钮),再次按下该标签时,不是突出显示按钮被按下(变暗)。我试过设置为NO属性adjustsImageWhenHighlighted,但它没有变暗,而是显示正常状态背景。
有什么建议吗?
编辑:这是我在UITabBarController子类中的代码
#import "MyTabBarViewController.h"
@interface MyTabBarViewController ()
@end
@implementation MyTabBarViewController
ExploreViewController *exploreController;
ProfileViewController *profileController;
UIButton* leftButton;
UIButton* rightButton;
- (void)viewDidLoad
{
[super viewDidLoad];
exploreController = [[ExploreViewController alloc] initWithNibName:@"ExploreViewController" bundle:nil];
profileController = [[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil];
self.viewControllers = [NSArray arrayWithObjects:exploreController, profileController, nil];
[self addLeftButtonWithImage: [UIImage imageNamed:@"LeftTabBarIcon"] highlightImage:[UIImage imageNamed:@"LeftTabBarIcon_On"]];
[self addRightButtonWithImage: [UIImage imageNamed:@"RightTabBarIcon"] highlightImage:[UIImage imageNamed:@"RightTabBarIcon_On"]];
}
- (void) leftTabPressed
{
leftButton.selected = YES;
rightButton.selected = NO;
[self setSelectedViewController:exploreController];
}
- (void) rightTabPressed
{
rightButton.selected = YES;
leftButton.selected = NO;
[self setSelectedViewController:profileController];
}
-(void) addLeftButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.adjustsImageWhenHighlighted = NO;
[[leftButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[leftButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[leftButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
[leftButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
leftButton.frame = CGRectMake(0.0, 367, 160.0, 49.0);
[leftButton addTarget:self action:@selector(leftTabSelectPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftButton];
}
-(void) addRightButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.adjustsImageWhenHighlighted = NO;
[[rightButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
rightButton.frame = CGRectMake(160.0, 367, 160.0, 49.0);
[rightButton addTarget:self action:@selector(rightTabPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightButton];
}
@end
答案 0 :(得分:2)
对于您的评论,您的代码将是这样的:
-(void) addRightButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.adjustsImageWhenHighlighted = NO;
rightButton.showsTouchWhenHighlighted = NO;
[[rightButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted | UIControlStateSelected];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
rightButton.frame = CGRectMake(160.0, 367, 160.0, 49.0);
[rightButton addTarget:self action:@selector(rightTabPressed) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *longGesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestureDetected:)]autorelease];
longGesture.delaysTouchesBegan = YES;
[rightButton addGestureRecognizer:longGesture];
[self.view addSubview:rightButton];
}
- (void)longGestureDetected:(UILongPressGestureRecognizer*)longGesture
{
if(longGesture.state == UIGestureRecognizerStateBegan)
[self rightTabPressed];
}
答案 1 :(得分:0)
Trick似乎正在将UIControlStateHighlighted
更改为UIControlStateHighlighted | UIControlStateSelected
。看起来很奇怪,但它确实有效。