Turn off highlighting on UIBarButtonItem - ios

I'm trying to use a UIBarButtonItem to put a title on my UIToolbar. I'm using the plain style and that looks fine, but I can't seem to get it to stop highlighting on touch. The Shows Touch When Highlighted option isn't available for the bar button items. Is there a quick and easy way to do this? I'm trying to do the building in interface builder so I can see what I'm doing. I'd prefer not to build the toolbar in the view did load every time.

The property responsible for this is accessible in the UIButton class:
myButton.showsTouchWhenHighlighted = NO;
You can access this (programmatically) in a UIBarButtonItem by assigning a UIButton to the bar button item's customView property, and configuring the button. You can do this in Interface Builder too: drag a UIButton onto a UIToolbar, and it will automatically embed it in a UIBarButtonItem for you - then look for the "Shows Touch On Highlight" checkbox under the button's settings.
Incidentally, I don't know how you're customising your buttons so feel free to ignore this, but if your button looks and behaves like a standard toolbar item then users will expect the glow effect.

I wanted a solution that could be used without any modification to my XIB structure.
The most obvious and simple one worked: subclass UIBarButtonItem:
UITitleBarButtonItem.h:
//
// UITitleBarButtonItem.m
// Created by Guillaume Cerquant - MacMation on 09/08/12.
//
/*
* A UIBarButtonItem that does not show any highlight on the touch
* Drag and drop a normal UIBarButtonItem in your xib and set its subclass to UITitleBarButtonItem
*/
#interface UITitleBarButtonItem : UIBarButtonItem
#end
UITitleBarButtonItem.m:
#import "UITitleBarButtonItem.h"
#implementation UITitleBarButtonItem
// Only caring about UITitleBarButtonItem set up in Interface Builder. Update this class if you need to instantiate it from code
- (void) awakeFromNib {
UIView *theView = [self valueForKey:#"view"];
if ([theView respondsToSelector:#selector(setUserInteractionEnabled:)]) {
theView.userInteractionEnabled = NO;
}
}
#end
Tested on iOS 5 and the one we aren't allowed to talk yet.

Alternative: Use a UIBarButtonItem in the plain style and additionally cover the toolbar in the appropriate area with a UIView that has a clear background. The view consumes the taps and hides them from the bar button item. Make sure you set the autoresizing mask correctly.

My solution was to set it to disabled, and adjust the titleAttributes for each UIControlState
let attributes: [NSAttributedStringKey: Any] = [
.font: UIFont.boldSystemFont(ofSize: 16),
.foregroundColor: UIColor.white
]
barButton.setTitleTextAttributes(attributes, for: .enabled)
barButton.setTitleTextAttributes(attributes, for: .disabled)
barButton.isEnabled = false

Related

UIBarButtonItem in navigation bar is highlighted when selected but UIBarButtonItem in toolbar is not

I have a simple UITableViewController with a NavigationController as the root view controller. I have added an "Options" UIBarButtonItem to the navigation bar and a "Start" UIBarButtonItem to the toolbar, shown below:
. The issue that I am experiencing is that the "Options" button will become highlighted when pressed, like a regular UIButton would, but the "Start" button on the toolbar does not. This is very inconvenient, as it makes it very hard for the user to know if they actually pressed the button or not. This behavior is shown below:
Options Button Not Pressed:
Options Button Pressed:
Start Button Not Pressed:
Start Button Pressed:
I can't figure out how to fix this behavior. I did verify that the "Start" button actually works, so the highlighting issue is not because the button is not working. Also, interestingly enough, the "Start" button does become highlighted when it is long pressed.
You simply misconfigured the UIbarButtonItem/UIButton in Interface Builder's Attributes Inspector.
There is no point to waste time investigating such a trivial issue, no matter how puzzling it may seem. Just delete that Start button and drop a new one into the toolbar again.
There is no reason for it to behave differently that the options button above.
You should change button type from UIButtonTypeCustom to UIButtonTypeSystem.
Make sure your button gets initialised with the .Default state in IB.
Then make sure the .Highlighted state Text Color is different than the .Default state Text Color.
Enable: Shows Touch On Highlight for tests.
There isn't a built-in way, but I can think of a few custom approaches:
Bind the button to a target method that toggles whatever the button
is meant to toggle, and then changes the button's image property
accordingly. You can use 2 images one for the selected state one
for the default.
Create your own subclass of UIBarButtonItem that looks something like
this:
#interface CustomBarButtonItem : UIBarButtonItem {
BOOL _state;
UIImage * selectedImage;
UIImage * defaultImage;
}
- (BOOL)toggleState;
#property (nonatomic, retain) UIImage * selectedImage;
#property (nonatomic, retain) UIImage * defaultImage;
#end
#implementation CustomBarButtonItem
- (BOOL)toggleState {
if (_state) {
// Switch to Off state
self.image = defaultImage;
}
else {
// Switch to On state
self.image = selectedImage;
}
return _state = !_state;
}
#end
is the toolbar set to translucent? I suspect your nav bar is and tool bar is not.

iOS custom shape navigation bar

I want to developer app with a custom navigation bar like in the following images:
I think that i need to subclass UINavigationBar and add button to centre of nav bar, but i don't really know how to make navigation bar look like on image. Can you please give me advice what should i do, links to any kind of documentation would be awesome!
Similar questions about navBar that doesn't helped me:
ios back button in the bar
Use custom Navigation Bar in iOS
Custom Navigation Bar in iOS 5
rogcar
EDIT:
My idea is next: make custom navigation bar height little bigger than default size, and add background image with arrow in it and with some transparency on the edges.
If you want a button (you probably do want) you can achieve it completely by subclassing UINavigationBar. You should remember that height of UINavigationBar is read-only property.
Style but not tappable:
So let's assume we subclass the navigation bar and add button there. You could do this and it will be going look great. For example:
- (void)drawRect:(CGRect)rect
{
self.backgroundColor = [UIColor lightGrayColor];
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2-50, 0 , 100, 100)];
[myButton setBackgroundColor:[UIColor lightGrayColor]];
[myButton setTitle:#"Normal" forState:UIControlStateNormal];
[myButton setTitle:#"Highlighted" forState:UIControlStateHighlighted];
[self addSubview:myButton];
[self sendSubviewToBack:myButton];
}
But you will facing a problem that your button is non tapeable below UINvaigationBar. (I post an image on the bottom of the answer)
So there is clearly not a path you want to follow. Don't even try that.
Style but not tappable 2:
You may override this method in your navigation bar subclass
- (CGSize) sizeThatFits:(CGSize)size {
return CGSizeMake(custom_width, custom_height);
}
And then mask it using UIBezierPath for example
The right (tappable) way:
You have to create a view stick to your UINavigationBar. What i will do here (if you want it to every screen) is:
Make a Category of UIViewController which can draw (for example - this is easiest way) UIButton.
Style this 'UIButton' whatever you want (if you want
Pin action to 'UIButton': [btn addTarget:self action:#selector(menuShow:) forControlEvents:UIControlEventTouchUpInside];
menuShow: method should be declare in your category
You can call drawing button every time you want to redraw view controller.
As you can see there there will be two separates View: UINavigationBar and UIButton. This is allow you to set content under this little button and make it tapable.
So why just don't hide navigation bar, and use different view? Because iOS7 ;) When Apple change it in iOS7 for example then you have to rebuild your pseudo NavigationBar, with only additional view, you don't need to do anything.
You do not need to subclass UINavigationBar. Create UIView add to it UIImageView as background with image in the shape you need, add button.
Subclass UINavigationController hide UINavigationBar, add custom navigation bar.
First Hide navigation bar using -
self.navigationController.navigationBarHidden = YES;
Then create UIView with required height,height of navigationBar is 44px.Then create background image view, object of required UIButton and add all objects on created UIView as a subview.It will look like navigationBar.Thank you.
You can add your custom shaped view as titleView on the navigation bar.
Just make sure that clipsToBounds is set to NO, so it doesn't get clipped.

iOS Buttons - add border

I am making my app ready for iOS7. I did conversion and was working with a user. The button in the app does not look like button. Looks very flat. Is there someway to put border or make it stand like a button?
Try this for adding border, It will work
#import <QuartzCore/QuartzCore.h>
then in viewDidLoad
_btn.layer.borderWidth=1.0f;
_btn.layer.borderColor=[[UIColor blackColor] CGColor];
_btn.layer.cornerRadius = 10;
also you can fill the color for making appearance somewhat like button, or best way is to use image there
Apart from BorderColor, you can do it by using Runtime attributes too.
try this , this will set border to button
#import <QuartzCore/QuartzCore.h>
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 10;
With Swift and XCode 6 you can do this.
Click the UIButton element in Storyboard, and go to identity inspector. In the user defined runtime attributes, enter:
layer.borderWidth number 1
If you want nice looking corners
layer.cornerRadius number 5
layer.maskToBounds boolean true
Now this will give you a border but to set the colour you need to do it with code. Go to your view controller, and add an IBOutlet from your button. Note that in this case it's an IBOutlet, not an IBAction. Say you do,
#IBOutlet weak var xButton: UIButton!
Call this in the viewDidLoad function like below to set the colour.
xButton.layer.borderColor = UIColor.whiteColor().CGColor
Thanks!
The design principles in iOS7 have changed. However, if you want to go flatter, but still want a custom button that "stands like a button", you can try out this open source component collection:
FlatUIKit on GitHub
There are two way to simplify button in ios 7
1>Set image : Just like Button using setImage Property for button
2>Set bordercolor borderWidth :
button.layer.borderWidth=1.0f;
button.layer.borderColor=[[UIColor blackColor] CGColor];
I suggest you try one of the iOS BootstrapButton libraries (or clones). Just change UIButton in the storyboard to BButton. There is just no preview in the storyboard.
http://github.com/katzlbt/iOSBootstrapButton
http://github.com/katzlbt/iOSBootstrapButtonDemo
http://github.com/mattlawer/BButton
If you use a background image, move button backward.
Editor>Arrange>send backward
In ios7 buttons are supposed to look borderless. Please see apple's View Transition Guidelines for help.
If you REALLY want the buttons to have a border, do: [button setImage:forState:] to set a customized button image with border.

Change Background Image of an iOS app with a button click

Title pretty much says it all, I am new to Xcode and am trying to make a button that changed the view's background image. I will have three button and when you click one they will change the background that is associated with the button. How would I accomplish this? I have looked around but have not found anything relevant or anything that worked..
Thanks in advance!
EDIT::
As for what I have, I have a project set up with a window and one view with a background image called "default#2x-568h#2x.png" I would like to be able to press a button and have it switch to "second.png". I have a button and I control dragged into my .h which made this:
#interface TeslameterViewController : UIViewController <CLLocationManagerDelegate> {
IBOutlet UIButton *button1;
}
- (IBAction)button1:(id)sender;
#end
thats where I am lost.
EDIT AGAIN:
Found the fix via this video:
http://www.youtube.com/watch?feature=player_embedded&v=iKuGiJMgMiQ
On the button Action event add this line.
(void)Action:(UIButton *)sender
{
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"bg.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
try this:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]];
and also see UIView Image Background for example project with complete code...
You need to be more specific on what you need.
Within your xib file, create a UIImageView and stretch its dimensions to fit the size of the screen. Also create a the necessary buttons. Create properties in your view controller to have a reference to the UIImageView and the buttons. Give the buttons an action that changes the picture that the UIImageView loads.
You have to create three buttons in nib file and add outlet to your view controller.
To create outlet you have right click on button then you will get list of event, then control drag to your view controller header file, then one small window will pop up, write method name and click enter. Now u will see method in your controller.write code to change background of button or your view in that method.
for more details:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphone101/Articles/05_ConfiguringView.html

UIBarButtonItem - Make it not clickable when just text

So I have a UIToolbar with 4 UIBarButtonItems on it (2 of them being Flexible Space) the middle one is of Style: Plain with a Title set "Settings" and then the far right button is a 'Done' button to close the view.
Everything looks good and works the way I want except for the middle text button is "clickable", it does nothing when you click on it but there is a white glow that appears around the text once it's touched.
The only options you get with a UIBarButtonItem is 'Enabled' which if turned OFF makes the text greyed out.
I was able to almost simulate the way it looks by creating a UILabel and adding as a subview to the UIToolbar but I was wondering if there's a simpler way to doing this.
Thanks
use the customView of UIBarButtonItem, to set an UILAbel as customView. the item will not be clickable.
UILabel *yourLabel = ...;
UIBarButtonItem *theBarItem = [[UIBarButtonItem alloc] initWithCustomView:yourLabel];
that's all.

Resources