tintColor of UISegmentedControl doesnt react - iphone

Come oon!
I am almost looking a day at this!
I want to set my tint color of my UISegmentedControl but it doesnt react...
NSArray *segControlItems = [NSArray arrayWithObjects:
#"L",
#"H",
#"A",
nil];
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.frame = CGRectMake(0, 0, 90, 30);
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.tintColor = [UIColor greenColor];
[segControl addTarget:self action:#selector(segAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segBarItem = [[UIBarButtonItem alloc] initWithCustomView:segControl];
self.switchView.navigationItem.rightBarButtonItem = segBarItem;
[segControl release];
[segBarItem release];
Has apple made some property like "makeUISegmentedControlsDontSetAnyTintColor" ore soo?
Please help...
A single button works: http://img257.imageshack.us/i/schermafbeelding2010102.png/
But with the segmented it's messed up: http://img714.imageshack.us/i/schermafbeelding2010102.png/

I've done it without problems using InterfaceBuilder. I had three segments, some with labels, some with labels + pictures, all working fine.
It must be your conversion into a BarButtonItem that doesn't work. You could try adding the segmented control as a subview of the nav bar instead of coaxing it into a button.

This is a really old question but I ran into this problem today (Xcode 7b5/iOS 9). In my case the segmentedControl was a UIBarButtonItem in the navBar. I was able to set the tintColor by waiting for the next run loop, like so:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] init]
(...additional setup...)
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
dispatch_async(dispatch_get_main_queue(), ^{
segmentedControl.tintColor=[UIColor purpleColor];
});

Related

Put self.editbuttonitem into segmentedcontrol as barbuttonitem?

I want to make a NavBar similar to the one in sample 3 of the NavBar sample code, except I want to use the self.editbuttonItem as one of the two buttons in the SegmentedControl. (The other will be a custom add button.) Basically - the end result will be a leftBarButtonItem that's just one button, bringing up a modal view, and a rightBarButtonItem that's a segmented control with both edit and add buttons.
Thing is, it looks like setting up the SegmentedControl needs an array of Strings or Images, but not BarButtonItems. Is there a workaround?
This is the relevant bit from Apple's sample:
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
Instead of the images, I want to put BarButtonItems...
If self.editbuttonitem is a UIBarButtonItem with title "Edit", I think you can do with the following code
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
#"Edit", #"AnotherButtonName"
nil]];
edit based on Charles Bandes's comment
Add an action to the segmentedControl, like the Apple's sample:
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//...
then in segmentAction:, do
- (void)segmentAction:(UISegmentedControl*)sender
{
//if the "edit" item in segmentedControl is selected
if (sender.selectedSegmentIndex == 0)
{
//I assume self is a UITableView instance
//start editing
[self setEditing:YES animated:YES];
}
}
I wrote those code on my PC. However it should work.
You may take a look at [UITableView setEditing:animated:]

How to add Segmented controll on UIToolBar in iphone

How to add segmented control on toolbar, I'm adding but not shows.
my code is: these code is on viewDidload
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0.0, 325.0, 320.0, 44.0)];
toolBar.barStyle = UIBarStyleDefault;
NSArray *segmentItem = [[NSArray alloc]initWithObjects:#"Day",#"List",#"Month", nil];
UISegmentedControl *segmentControll = [[UISegmentedControl alloc]initWithItems:segmentItem];
segmentControll.frame = CGRectMake(80.0, 325.0, 200.0, 30.0);
//[self.toolBar addSubview:segmentControll];
[self.navigationController.toolbar addSubview:segmentControll];
[self.view addSubview:toolBar];
You can add only the UIBarButtonItem's to UIToolbar.
First, add the segmentControll to a bar button item,
UIBarButtonItem *segBarBtn;
segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
Add the bar button to the tool bar,
NSArray *toolbarItems = [NSArray arrayWithObject:segBarBtn];
[toolbar setItems:toolbarItems animated:NO];
1. self.navigationController.toolbar is not the same as toolBar.
2. You're doing it wrong. You should add segmentControl as UIBarButtonItem:
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControll];
toolBar.items = [NSArray arrayWithObject:btnItem];
Try something like this
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0.0, 325.0, 320.0, 44.0)];
toolBar.barStyle = UIBarStyleDefault;
NSArray *segmentItem = [[NSArray alloc]initWithObjects:#"Day",#"List",#"Month", nil];
UISegmentedControl *segmentControll = [[UISegmentedControl alloc]initWithItems:segmentItem];
segmentControll.frame = CGRectMake(80.0, 325.0, 200.0, 30.0);
//[self.toolBar addSubview:segmentControll];
UIBarButtonItem *toolbarItem = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
[toolBar addSubview:toolbarItem];
[self.view addSubview:toolBar];
With a regular UIToolbar you do so by making the segmented control a part of a custom UIBarButtonItem like this.
UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentControll];
NSMutableArray *itemCopy = [self.toolbar.items mutableCopy];
[itemCopy addObject:segmentBarItem];
self.toolbar.items = itemCopy;
[itemCopy release];
[segmentBarItem release];
But in your case you are trying to modify the toolbar of a navigation controller which is discouraged.
This property contains a reference to the built-in toolbar managed by
the navigation controller. Access to this toolbar is provided solely
for clients that want to present an action sheet from the toolbar. You
should not modify the UIToolbar object directly.
You will still want to create a custom UIBarButtonItem but you will want to set it as the rightBarButtonItem

What to do when UIBarButtonItem are not visible in UINavigationBar?

Hello Everyone
I am writing a simple code in ViewDidLoad, and class is child of UITableViewController, like below but buttons are not visible, while title is visible,
Another thing that I am clicking a button which in ViewDidLoad method ViewController.m, and which is call a method, code of that method is as below
//Code of button target method
-(void)statusMethod {
NSLog(#"statusMethod");
Status *ob=[[Status alloc]init];
[self presentModalViewController:ob animated:YES];
}
//Code of ViewDidLoad of Status.m
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, width, 43)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:navBar];
[navBar release];
UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithTitle:#"HOME" style:UIBarButtonItemStylePlain target:self action:#selector(homeMethod)];
self.navigationItem.rightBarButtonItem = home;
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonSystemItemAdd target:self action:#selector(addMethod)];
self.navigationItem.leftBarButtonItem = add;
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(10,10,width-20,25)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = #"Status";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:25];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];
[label release];
Any Idea, what is problem in code?
I you don't understand, then u can ask me again,
I will praise, if I will get solution of my problem
It seems like self.navigationItem is only looked at by the UINavigationController (controller, not bar). The bar itself isn’t going to check that.
So you should either
1) Use a real UINavigationController which comes with its own navigationBar and will handle the navigationItems for you. The self.navigationItem code above will work in that case. (you should consider self.navigationItem.title = #"Status";)
If your UITableViewController is going to be pushing stuff on and off the navigation stack, this is the path you should take in any case.
2) Use the UINavigationBar’s own navigation item. Except it doesn’t seem to come with a navigation item, so you have to add your own:
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Status"];
[navBar setItems:[NSArray arrayWithObject:navItem]];
[navItem release];
and then set the left and right buttons as
[navBar topItem].leftBarButtonItem = add;
[add release];
[navBar topItem].rightBarButtonItem = home;
[home release];
Dealing with UINavigationController/UINavigationBar/UINavigationItem can be confusing, but luckily Apple has a decent explanation of how all these things work together at the top of its UINavigationController documentation.

How can I add two UIBarButtonItems to UINavigationItem?

I want two rightBarButtonItem's on my UINavigationBar. How can I accomplish this?
You can use a UISegmentedControl with two buttons and configure it with the momentary property set to YES.
This is what is used in the Mail application to go to next/previous message.
Update
In order to assign the UISegmentedControl]1 as a right button, you have to wrap it inside a UIBarButtonItem (sample code taken from the NavBar sample application):
- (void)viewDidLoad
{
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
}

How can I fix this NavigationController and UIToolbar offset issue in Objective-C?

I'm adding a couple of buttons to an already-existing NavigationController. The two buttons are added to a UIView, which is pushed onto the NavigationItem. The buttons stop and reload a UIWebView.
Problem is that there's a slight offset issue that is making it all look pretty ugly. I wish I could set the UIToolbar to transparent or clear background but that doesn't seem to be an option. Can't seem to use negative offsets either. I've got color matching, but if you look closely there's 1px or 2px of highlighting up top that's causing a visual mismatch and then a slight offset at the bottom.
Some relevant code below (based on this, inbound Googlers). What are my options to resolve this?
// create a toolbar for the buttons
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleDefault];
UIColor *colorForBar = [[UIColor alloc] initWithRed:.72 green:0 blue:0 alpha:0];
toolbar.tintColor = colorForBar;
[colorForBar release];
//[toolbar setTranslucent:YES];
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard reload button
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(reload)];
reloadButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:reloadButton];
[reloadButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIBarButtonItem *stopButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:#selector(stopLoading)];
stopButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:stopButton];
[stopButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
[toolbar release];
Update: Alignment was fixed by suggestion by #Jacob below. Now, how to solve the highlighting mismatch?
I've had this same exact issue before, and after some hacking around, I figured this out:
You need to set the height of the UIToolbar's frame to 44.01 for this to work.
EDIT: Re: highlighting mismatch -> You can try setting the backgroundColor to the colorForBar.
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 44.01)];
UIColor *colorForBar = [[UIColor alloc] initWithRed:.72 green:0 blue:0 alpha:0];
toolbar.tintColor = colorForBar;
toolbar.backgroundColor = colorForBar;
Instead of putting the buttons into a toolbar, you might just make them children views of a parent UIView. You can position a button directly through its frame or center property, without use of spacers, etc.