0

さて、私は3つのUIButton(タブをシミュレート)を備えたカスタムuitabbarcontrollerを持っています。通常の状態、選択された状態、強調表示された状態の背景画像を設定しました(選択された状態と強調表示された状態はどちらも同じです)。すべてが正常に機能しますが、1つだけあります。タブ(ボタン)を選択しているときに、そのタブを強調表示する代わりにもう一度押すと、ボタンが押されている(暗くなる)ことが示されます。プロパティ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

トリックはに変わっUIControlStateHighlightedているようUIControlStateHighlighted | UIControlStateSelectedです。奇妙に思えますが、機能します。

于 2012-12-22T06:48:34.483 に答える