5

Nie jestem pewien, co robię źle. Nazwa pliku jest poprawna, styl jest ustawiony na zwykły. Ale dostaję bankowe białe pudełko wielkości mojego obrazu. Używam UINavigationController.Problem z dodaniem obrazu do paska narzędzi za pomocą UIBarButtonItem, wyświetlając puste białe pole zamiast obrazu

Prosimy o pomoc i dziękuję z góry dziękuję.

** FYI Jestem zupełnie nowy dla obiektywnego c, więc nie bądź dla mnie zbyt surowy. ;)

UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc] 
    initWithImage:[UIImage imageNamed:@"channel-guide-button.png"] 
    style:UIBarButtonItemStylePlain 
    target:self 
    action:@selector(action:)]; 


self.toolbarItems = [NSArray arrayWithObjects:toolbarChannelGuideButton, nil]; 
[toolbarChannelGuideButton release]; 

Odpowiedz

9

Powodem było stworzenie białą maskę było ponieważ UIToolBar robi pozwalają kolorowych obrazów na nią domyślna. Aby to osiągnąć, należy utworzyć UIImage, a następnie przypisać do tego obrazu UIButton. Następnie utwórz UIBarButton, używając initWithCustomView z UIButton jako niestandardowym widokiem.

Kod:

 //Load the image 
    UIImage *buttonImage = [UIImage imageNamed:@"your-image.png"]; 

    //create the button and assign the image 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setImage:buttonImage forState:UIControlStateNormal]; 

    //sets the frame of the button to the size of the image 
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height); 

    //creates a UIBarButtonItem with the button as a custom view 
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 



    self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil]; 
    [customBarItem release]; 
+0

dzięki! To działało idealnie ... – manderson

+0

UIButtonTypeCustom naprawiono to dla mnie. Używałem zaokrąglonego – coolcool1994

-1

Czy kanał-przewodnik-przycisk.png należy do projektu?

Można przerwać ten się tak:

UIImage *image = [UIImage imageNamed:@"channel-guide-button.png"]; 
NSLog(@" image = %p", image); 
UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc] 
    initWithImage:image 
    style:UIBarButtonItemStylePlain 
    target:self 
    action:@selector(action:)]; 

lub po prostu sprawdzić swój projekt ;-)

+0

Sprawdziłem w moim projekcie i tak jej tam. Uruchomiłem też komunikat NSLog, który wyświetlał niski numer. –

+0

image = 0x6b45340 jest tym, co wyświetlano na konsoli –

+0

No cóż. FWIW, zwykle tworzę UIBarButtonItem z initWithCustomView: i przekazuję UIButton z obrazem z nim powiązanym. Wczytuję przykład kodu jutro. – westsider

1

Począwszy iOS 7 można użyć poniżej:

UIImage *image = [[UIImage imageNamed:@"myImage.png"]; 
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)]; 
Powiązane problemy