UIButton has dot when set to selected inside UIToolbar - ios

I'm adding a UIButton to a UIToolbar that I don't want to have as a UIBarButtonItem so that I have more control over the position of the UIButton inside the frame. I set a different title for both "Normal" and "Selected" states. Everything acts completely normal for a UIButton except when it's "Selected" there is a 1 pixel dot in the front of the UIButton's title.
toolbar:
UIToolbar *customView = [[UIToolbar alloc] initWithFrame:CGRectMake(HEADER_MARGIN, 0, HEADER_WIDTH, HEADER_HEIGHT)];
[customView setBarTintColor:[UIColor whiteColor]];
button:
UIButton *listOrderButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ORDER_BUTTON_WIDTH, ORDER_BUTTON_HEIGHT)];
[listOrderButton setCenter:CGPointMake(customView.center.x, HEADER_HEIGHT/2)];
[listOrderButton.titleLabel setFont:[UIFont systemFontOfSize:ORDER_BUTTON_FONT_SIZE]];
[listOrderButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[listOrderButton setTitle:#"ABC" forState:UIControlStateNormal];
[listOrderButton setTitle:#"Time" forState:UIControlStateSelected];
[listOrderButton setTitleColor:[UIColor blueAppColor] forState:UIControlStateNormal];
[listOrderButton addTarget:self action:#selector(listOrderChange:) forControlEvents:UIControlEventTouchUpInside];
[customToolbar addSubview:listOrderButton];
and the selector:
- (void)listOrderChange:(UIButton *)sender {
[sender setSelected:!sender.selected];
}
Image of what's happening:
- No dot when not selected
- When selected, there is a dot
This happens both on the device and in the simulator. Changing the text color for any and all states has no bearing on the color of the dot itself.
I should also point out that the dot is two points wide and one point high.

Instead of using UIButton, I know you dont want to use a UIBarButton, but would it be possible to create a UIBarButton programmatically giving it all the features of a UIButton?
Since UITabBar is like UINavigation Bar and apple suggests using UIBarButtonItem for bar/tabBar Buttons... I believe using a UIBarButtonItem might solve the problem....

Related

Navigation bar with UIImage Button and subtitle

I want to recreate something like the below image which is navigation bar with UIImage buttons and subtitle.
My current code looks something like in below
UIImage * cropModeImage = [UIImage imageNamed:#"crop"];
UIButton *cropModeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cropModeButton setImage:[cropModeImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[cropModeButton addTarget:self action:#selector(_setCropMode:) forControlEvents:UIControlEventTouchUpInside];
UILabel *subtitle = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 185, 30)];
[subtitle setBackgroundColor:[UIColor clearColor]];
[subtitle setFont:[UIFont fontWithName:#"Helvetica" size:14]];
subtitle.text=#"This is the sub";
[subtitle setTextColor:[UIColor blackColor]];
[cropModeButton addSubview:subtitle];
[self.view addSubview:cropModeButton];
UIBarButtonItem *cropMode = [[UIBarButtonItem alloc] initWithCustomView:cropModeButton];
Can anyone help me with it?
In iOS, this is done using tabbar controller which generally appears at the bottom of the screen and if you want to show on the top of your screen then use the toolbar.
Look at the calendar app of iPhone, it has 3 buttons to the right side and that's how it will look if you add buttons in the navigation bar.
I would prefer using the storyboard OR XIB for this and add all the barbutton items there and assign IBAction methods to it, here's an example of how you can use storyboard for the same.
The buttons used in the toolbar can have images with captions to display the same look and feel.
Hope it helps.

Customize Navigation Bar in ios

I used Navigation Controller & navigation Bar in StroryBoard.
I want to customize navigation Bar for one view as follow for right side of navigation bar.
My tried code
UIButton *btn_list=[UIButton buttonWithType:UIButtonTypeCustom];
btn_list.frame=CGRectMake(0, 0, 60, 30);
[btn_list setTitle:#"Liste" forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"red-left.png"] forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"black-left.png"] forState:UIControlStateSelected];
UIBarButtonItem *list_bar=[[UIBarButtonItem alloc]initWithCustomView:btn_list];
UIButton *btn_map=[UIButton buttonWithType:UIButtonTypeCustom];
btn_map.frame=CGRectMake(0, 0, 60, 30);
[btn_map setTitle:#"Karte" forState:UIControlStateNormal];
[btn_map setBackgroundImage:[UIImage imageNamed:#"red-right.png"] forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"black-right.png"] forState:UIControlStateSelected];
UIBarButtonItem *map_bar=[[UIBarButtonItem alloc]initWithCustomView:btn_map];
self.navigationItem.rightBarButtonItems=[[NSArray alloc]initWithObjects:list_bar,map_bar, nil];
Problem of tried code: There is a space between two buttons.
How can I achieve this?
Just use Segmented Control: you have a full example HERE
declare a segment control with custom view
UISegmentedControl *control = (UISegmentedControl *) [segmentBarButton customView];
and then add it to barbuttonitem view
UIBarButtonItem *segmentBarButton=[[UIBarButtonItem alloc] initWithCustomView:control];
dont forget to write the lines that relates to segment control customized view like ur requirement adding red or black images to the segment 0 or 1.

Programmatically created UIButton not visible

I have created a UIButton, added it to a view, which has then been added to a UIScrollView. This View also contains a UITextView which displays and is formatted correctly.
My code is below. Hopefully someone can help. The area where the button should be is clickable, and acts correctly, however the button is not visible. I even changed the tintColor to red, but I cannot see it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *billBlocker = *UIView initialized*
//The UITextView is a subview of `billBlocker`, as well as the `billBlockButton`
//added the UITextView here
UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30 )];
[billBlockButton setTitle:#"BillBlockerFrenzy" forState:UIControlStateNormal];
[billBlockButton setTintColor:[UIColor redColor]];
[billBlockButton addTarget:self action:#selector(segueToRegulationsGame) forControlEvents:UIControlEventTouchUpInside];
[billBlocker addSubview:billBlockButton];
[self.scrollView addSubview:billBlocker];
}
You should use cluster method when crate button:
UIButton *billBlockButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[billBlockButton setFrame:CGRectMake(0, 5,292,30 )];
...
This code should work. Make sure the scrollView frame and content size is set correctly and scrollView is added to self.view.
buttonWithType: is recommended method for creating the button, this is the only way you can specify button type.
When you use alloc init, I believe, you use some standard button type and if iOS change the standard also could change.
Probably the problem is you forgot the set outlet of scroll view i tried and its working like this
#property (strong,nonatomic) IBOutlet UIScrollView *sView; //scrollviews outlet
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//added the UITextView here
UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30 )];
[billBlockButton setTitle:#"BillBlockerFrenzy" forState:UIControlStateNormal];
[billBlockButton setBackgroundColor:[UIColor redColor]];
[billBlockButton addTarget:self action:#selector(nothing) forControlEvents:UIControlEventTouchUpInside];
[self.sView addSubview:billBlockButton];
}
Forgot to say you need to add your method instead of nothing method..
Hope it helps.
When we create the UIButton object programmatically, the default title color of UIButton is white.
So, set the title color of UIButton object.
[billBlockButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
iOS 7 or later will have transparent button. Unless you set text, you will not be able to see. Do following:
[[yourButton layer] setBorderColor:[UIColor blackColor].CGColor];
[[yourButton layer] setBorderWidth:2.0];
[yourButton setTitle:#"Hello" forState:UIControlStateNormal];

iOS Custom Font "Floats" Too High

I'm creating a custom UIBarButtonItem, like so:
UIImage *originalImage = [UIImage imageNamed:#"button"];
UIImage *buttonImage = [originalImage stretchableImageWithLeftCapWidth:10 topCapHeight:5];
UIButton *toolbarB = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65, 29)];
[toolbarB setTitle:title forState:UIControlStateNormal];
[toolbarB.titleLabel setFont:[UIFont fontWithName:kLatoBold size:17.0f]];
[toolbarB setBackgroundImage:buttonImage forState:UIControlStateNormal];
[toolbarB addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarB];
Depending on which of my custom fonts I use, after inserting my UIBarButtonItem into my controller's navigationItem, I get one of the results below.
Why does the button text in the second result "float" higher than it is supposed to? The first button looks great, but the second one positions the text in an unnatural fashion... Could it be a problem with my font?
Button Text Displaying Properly
Annoying, Floating Button Text that is Too High!
It happens for some fonts, I don't know why, but I can suggest you to use UIEdgeInsets to push your text down a bit. I guess you cannot fix your font, so;
Here is a link for a similar solution; Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

UIBarButtonItem with image and text, and a border?

I would like to create a UIBarButtonItem with an icon and text. I've seen this solution and this one but when you set the background image it doesn't draw a border.
I don't really want to draw a border (that looks like the "Plain" style one) with my icon on it. Is there another way around this?
If you look at the first solution you linked to you will notice the following line
UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:chatButton] autorelease];
You can initialize a button with whatever view you would like. You can make a button with any (acceptable) view inside it.
eg.
// GRAPHICAL BUTTON FRAMEWORK
UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:#selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:#"SOME IMAGE"] forState:UIControlStateNormal];
UIBarButtonItem* remix = [[[UIBarButtonItem alloc] initWithCustomView:btton] autorelease];
Hope that helps!

Resources