0

好吧,我有一个自定义的 uitabbarcontroller,上面有 3 个 UIButtons(模拟选项卡)。我已经为正常、选定和突出显示的状态设置了背景图像(选定和突出显示都是相同的)。一切正常,但有一件事:当我选择了选项卡(按钮)时,该选项卡再次被按下,而不是突出显示它显示按钮被按下(变暗)。我尝试将属性 adjustsImageWhenHighlighted 设置为 NO,但它并没有变暗,而是显示了正常状态背景。

有什么建议吗?

编辑:这是我在 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
4

2 回答 2

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];
}
于 2012-04-15T12:55:33.247 回答
0

Trick 似乎正在更改UIControlStateHighlightedUIControlStateHighlighted | UIControlStateSelected. 看起来很奇怪,但它确实有效。

于 2012-12-22T06:48:34.483 回答