Changing the UIBarButtonItem View for Transparent Software

I had problems getting this to work and I haven’t seen a working example anywhere on the Internet. Now I offer generosity on this, as it makes me crazy. It should be easy, but it doesn't seem to be.

I would like my buttons on my UINavigationBar to be translucent so that they display the background of what is on the UINavigationBar to show. This effect manifests itself in many applications below. You can do this by setting a custom background on an element, which I think is an unacceptable solution, because it requires you to prepare images in advance, and they will not be adapted for variable buttons, etc. They will not look like the Apple user interface and I do not believe that there is a reason for this: UIKit already draws a background for these buttons, we just need to change it. The correct solution uses bar elements and views created by Apple apis.

UIBarButtonItem is not a subclass of UIView. When you create it and add it to the UINavigationBar, some code somewhere in the structure draws a view for it. Framework methods seem to resist anything related to ensuring the transparency of bar elements, such as the tintColor property.

For example, this does NOT work:

UINavigationItem *item = [[UINavigationItem alloc] init]; UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"SUCKS" style:UIBarButtonItemStyleBordered target:self action:@selector(whatever:)]; editButton.tintColor = [UIColor colorWithWhite:0.4 alpha:0.3]; item.leftBarButtonItem = editButton; 

None of this will make the UINavigationBar allow translucency for its bar elements. I believe that at runtime we should:

  • Get image for panel item
  • Mask for transparency
  • Set a new image in the panel

But I was not able to get the image at runtime or disguise it properly. How do you do this?

Like this

+6
source share
5 answers

Create a custom uiview and draw a translucent black rectangle in it and use this view with initWithCustomView.

see and

Otherwise, you may need to use an image (png). e.g. 1x1 black pixel png with 30% opacity. Then you can execute initWithImage.

EDIT: I had this second approach using:

 buttonThree = [[UIBarButtonItem alloc] initWithTitle:@" sort button " style:UIBarButtonItemStyleBordered target:self action:@selector(sortMethod)]; UIImage *thebgUIimage = [UIImage imageNamed:@"semi.png"]; [buttonThree setBackgroundImage:thebgUIimage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

As a result, a button with a transparent background image appears that shows the background image of the navigator. However, you need to create an image with rounded corners and therefore need an image for each button width. I also found this thread after trying to use the above

+5
source

A brilliant hack is to use the UISegmentedControl with one segment (like a button) and set the color of the hue. Take a look at http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem . I personally did it. Feel free to ask any questions.

+2
source

Instead of looking for code and racking my brains, my suggestion is to have a transparent image that has only a border that looks like a button (add a shadow if necessary), create a custom type button, add a transparent background image to it and you can text as you want. On this custom button, create your panel button element accordingly.

0
source

If you target iOS 5, you can set the background image of the button.

  [_button setBackgroundImage:@"image" forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

Note that you need to set background images for the UIControlSateSelected state and again for both control states for barMetrics: UIBarMetricsLandscape if your application allows landscape orientation.

Please note that this is a feature of iOS 5.

0
source

Source: https://habr.com/ru/post/905533/


All Articles