2009-12-20 8 views
4

원하는 것 : UIButton이 선택되었는지 여부를 나타내는 테두리.UIButton 테두리 및 배경 이미지

배경 : 텍스트가 아닌 투명 이미지를 사용하는 일부 UIButton이 있습니다. 이들은 토글 버튼입니다 (예 : 켜기 또는 끄기).

문제점 : UIButton 클래스는 사용자가 단추에 대해 다른 것을 변경하지 않으면 단추를 선택했는지 여부를 알 수 없습니다. 상태에 따라 이미지가 변경되지 않으므로 모든 이미지 중 두 개, 보통 이미지 하나, 선택된 이미지 하나, 버튼 상태별로 하나씩 설정해야합니다. 이것은 성가신 일입니다. 대신 배경 이미지를 변경하겠다고 생각했는데 버튼의 예쁜 테두리가 제거되었으므로 배경 이미지가 사각형이되었습니다. 내가 좋아하지 않는

가능한 해결책 :

1)있는 UIButton 경계를 일치하는 배경을 만들고 선택을 위해 그것을 사용합니다. 나는 그들이 완벽하게 일치하지 않을 것이기 때문에 나는 이것을 좋아하지 않는다. 나는 까다 롭다.

2) 본질적으로 동일하지만 배경이 다른 각 버튼에 대해 두 개의 이미지를 만듭니다. 이것은 불필요한 작업 인 것처럼 보이며이 문제가 반복적으로 발생하고 있기 때문에 앞으로의 솔루션을 원합니다.

누군가가 지금까지 괜찮은 해결책을 찾았 으면합니다. 미리 감사드립니다.

답변

4

UIButton에는 이미지와 배경 이미지라는 두 개의 이미지 레이어가 있기 때문에 모든 단추에 대해 두 개의 배경 이미지 만 사용하여 원하는 것을 얻을 수 있다고 생각합니다. 한 이미지는 테두리를 표시하고 다른 이미지는 테두리를 표시하지 않습니다. 컨트롤 상태가 변경되면 배경을 바꿔 넣습니다.

+0

이 최선의 선택처럼 보인다. 나는 잘 어울리는 두 개의 배경을 만들고, 규칙적인 UIButton 모양을 밀접하게 따른다. –

0

국경 만 원할 경우 두 가지 상태에 대해 두 개의 이미지를 사용하도록 선택할 수 있습니다. 그렇지 않으면 두 가지 상태를 구별하는 것이 목적이라면 선택한 부분 조금만 알파를 변경하여 할 수 있습니다 버튼을 누르면 토글 버튼과 같은 효과가 나타나며 선택한 버튼을 비활성화하고 다른 버튼을 누르면 다시 활성화 할 수 있습니다.

희망은 이것이 당신에게 좋은 아이디어가 될 것입니다.

1
// 
// TabBarSingleton.h 

#import <Foundation/Foundation.h> 

@interface TabBarSingleton : UITabBarController <UITabBarControllerDelegate>{ 
    NSRecursiveLock *barLock; 

    UIButton *Button; 
    UIButton *favoriteButton; 
} 

@property(nonatomic, retain) UIButton *Button; 
@property(nonatomic, retain) UIButton *favoriteButton; 

- (void) ButtonPressed; 
- (void) favoriteButtonPressed; 

@end 

/////////////////////////////////// 
0
// 
// TabBarSingleton.m 

// Created by ArunDhwaj on 9/7/10. 
// Copyright 2010 __MyCompanyName__. All rights reserved. 
// 


#import "TabBarSingleton.h" 

@implementation TabBarSingleton 

@synthesize Button, favoriteButton; 


- (id) init 
{ 
    if (self = [super initWithNibName: nil bundle: nil]) 
    { 
     barLock = [[NSRecursiveLock alloc] init]; 
    } 
    self.delegate = self; 
    return self; 
} 

+ (TabBarSingleton *) defaultBar 
{ 
} 

- (void)viewDidLoad 
{ 
    NSLog(@"TabBarSingleton: viewDidLoad"); 

    //Hiding TabBar 
    self.tabBar.hidden = YES; 

    //Creating a UIView, its frame is same as tabBar frme 
    CGRect tabbarFrame = self.tabBar.frame; 
    UIView* customTabbarView = [[UIView alloc] initWithFrame:tabbarFrame]; 

    UIImageView *newsFeedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newsfeeds_normal.png"]]; 
    newsFeedImg.frame = CGRectOffset(newsFeedImg.frame, 0, 1); 

    Button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [Button setFrame:newsFeedImg.frame]; 

    [Button setBackgroundImage:newsFeedImg.image forState:UIControlStateNormal]; 
    [Button setBackgroundImage:[UIImage imageNamed:@"newsfeeds_active.png"] forState:UIControlStateHighlighted];  
    [Button addTarget:self action:@selector(newsFeedsButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 

    [customTabbarView addSubview:Button]; 
    //[newsFeedImg release]; 

    CGRect newsFeedFrame = newsFeedImg.frame; 
    UIImageView *favoriteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favorites_normal.png"]]; 
    favoriteImg.frame = CGRectMake(newsFeedFrame.size.width, newsFeedFrame.origin.y, newsFeedFrame.size.width, newsFeedFrame.size.height); 

    favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [favoriteButton setFrame:favoriteImg.frame]; 

    [favoriteButton setBackgroundImage:favoriteImg.image forState:UIControlStateNormal]; 
    [favoriteButton setBackgroundImage:[UIImage imageNamed:@"favorites_active.png"] forState:UIControlStateHighlighted];  
    [favoriteButton addTarget:self action:@selector(favoriteButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 

    [customTabbarView addSubview: favoriteButton]; 
    //[favoriteImg release]; 

    [self.view addSubview:customTabbarView ]; 

    [self newsFeedsButtonPressed]; 
} 


- (void) newsFeedsButtonPressed 
{ 
    NSLog(@"TabBarSingleton: newsFeedsButtonPressed"); 
    self.selectedIndex = 0; 

    //Keeping Highlighted newsFeed tab 
    UIImageView *newsFeedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newsfeeds_active.png"]]; 
    [Button setImage: newsFeedImg.image forState:UIControlStateNormal]; 

    //Keeping normal others tab icons 
    UIImageView *favoriteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favorites_normal.png"]]; 
    [favoriteButton setImage: favoriteImg.image forState:UIControlStateNormal]; 
} 

- (void) favoriteButtonPressed 
{ 
    NSLog(@"TabBarSingleton: favoriteButtonPressed"); 
    self.selectedIndex = 1; 

    //Keeping Highlighted newsFeed tab 
    UIImageView *newsFeedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newsfeeds_normal.png"]]; 
    [Button setImage: newsFeedImg.image forState:UIControlStateNormal]; 

    //Keeping normal others tab icons 
    UIImageView *favoriteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favorites_active.png"]]; 
    [favoriteButton setImage: favoriteImg.image forState:UIControlStateNormal]; 

#pragma mark UITabBarControllerDelegate 

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{ 
    NSLog(@"TabBarSingleton: shouldSelectViewController"); 
    return YES; 
} 

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{ 
    NSLog(@"TabBarSingleton: didSelectViewController"); 
} 

- (void) dealloc 
{ 
    //[barLock release]; 
    [super dealloc]; 
} 

@end 
+1

문제를 해결하는 방법을 설명하는 설명이나 코드를 코드에 추가해야합니다. 그것은 보통 여기에서 선호됩니다. 물론 모든 기능을 갖춘 코드 스 니펫은 훌륭한 추가 기능이지만 그 자체로는 솔루션을 이해하는 사람이 질문을 제기하는 데 도움이되지 않습니다. –