I have used the following code to create a segmented controller, but I can't differentiate which is selected and which is not selected. How do I differentiate?
UISegmentedControl *segmentedControl;
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:#"Male" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Female" atIndex:1 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(100,10,200,30);
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
- (void)segmentSwitch:(id)sender
{
segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
UIView *firstView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20,20)];
firstView.backgroundColor=[UIColor greenColor];
UIView *secondView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20,20)];
firstView.backgroundColor=[UIColor brownColor];
if (selectedSegment == 0)
{
NSLog(#"first segment");
//toggle the correct view to be visible
strGender =[[NSMutableString alloc]initWithString:#"Male"];
[firstView setHidden:NO];
[secondView setHidden:YES];
}
else
{
NSLog(#"second segment");
//toggle the correct view to be visible
strGender =[[NSMutableString alloc]initWithString:#"Female"];
[firstView setHidden:YES];
[secondView setHidden:NO];
}
}
There are a few things here. Based on your comment to #Surjit's answer, you will have to use insertSegmentWithImage:atIndex:animated if you want to change the color of the segment. You will need to have images for each segment for both selected and non-selected state.
But there are few problems in your segmentSwitch: method. You are creating both firstView and secondView but not adding them to the view hierarchy. You are setting the background color of firstView twice. You probably intended one of the calls to be to secondView. And there's no point changing the hidden property of the two views without them being on the screen. If you are looking to switch between two views of different colors, then declare them as ivars and initialize them elsewhere and then switch their hidden on segment switch.
UISegmentedControl *segmentedControl;
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(0, 6, 320, 40);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.backgroundColor = [UIColor blackColor];
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
// here is the code how to differenciate selected , non selected section on segmented //controller
int selectedSegment = segmentedControl.selectedSegmentIndex;
if(selectedSegment == 0)
{
// code 1
}
else if(selectedSegment == 1)
{
// code 2
}enter code here
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UISegmentedControl *segmentedControl; // add this to your (.h) file
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:#"Red" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Green" atIndex:1 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Blue" atIndex:2 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(0, 0, 320, 40);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.backgroundColor = [UIColor blackColor];
[segmentedControl setMomentary:NO]; // imp property (change it & see magic)
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
}
(IBAction)segmentSwitch:(id)sender {
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0)
self.view.backgroundColor = [UIColor blueColor];
else if (selectedSegment == 1)
self.view.backgroundColor = [UIColor greenColor];
else if (selectedSegment == 2)
self.view.backgroundColor = [UIColor redColor];
}
Just copy & paste this code.
see this example here i am setting tint color for selected
and unselected segment index.
But before that please unchecked the momentary state from xib
for UISegmentedControl.
- (void)segmentAction:(id)sender{
UIColor *tintcolor1=[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1.0];
UIColor *tintcolor2=[UIColor colorWithRed:211/255.0 green:78/255.0 blue:65/255.0 alpha:1.0];
for (int i=0; i<[segment_controller.subviews count]; i++)
{
if ([[segment_controller.subviews objectAtIndex:i]isSelected] )
{
[[segment_controller.subviews objectAtIndex:i] setTintColor:tintcolor2];
}else
{
[[segment_controller.subviews objectAtIndex:i] setTintColor:tintcolor1];
}
}
Related
I have many textViews on top of a scrollView and every textView has a custom button over it so that when user click that textView it should expand and when it click back then it should collapse to previous position.
what I'm thinking of doing is hide the smallTextView and show the expandedTextView when button is pressed and when the button is pressed i want to hide expandedtextView n show the smallTextView. but I don't know how i should do it. any help will be appreciated.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"Demo";
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for (int i=0;i<[appDelegate.serverResponseArray count];i++)
{
self.expandTextView = [[UITextView alloc] init];
[self.expandTextView setFrame:CGRectMake(8.0f, i*50.0f+10.0f, 270.0f, 40.0f)];
[self.expandTextView setBackgroundColor:[UIColor grayColor]];
[self.expandTextView setFont:[UIFont fontWithName:#"helvetica" size:12]];
[self.expandTextView setText:#"Welcome!!!"];
[self.expandTextView setTextColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1]];
[self.expandTextView setUserInteractionEnabled:NO];
[self.scrollView addSubview:self.expandTextView];
self.expandTextView = nil;
self.expandButton = [[UIButton alloc]initWithFrame:CGRectMake(8.0f, i*50.0f+1.0f, 270.0f, 60.0f)];
[self.expandButton setBackgroundColor:[UIColor clearColor]];
[self.expandButton addTarget:self action:#selector(textButtonClicked:) forControlEvents:UIControlEventTouchDragInside];
[self.scrollView addSubview:self.expandButton];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(278.0f, i*50.0f+10.0f, 14.0f, 40.0f)];
[imageView setBackgroundColor:[UIColor grayColor]];
[imageView setImage:[UIImage imageNamed:#"arrow.png"]];
[self.scrollView addSubview:imageView];
imageView = nil;
}
float maxHeight = 0;
for(UIView *v in [self.scrollView subviews])
{
if(v.frame.origin.x + v.frame.size.height > maxHeight)
maxHeight = v.frame.origin.x + v.frame.size.height;
}
self.scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, maxHeight+2570);
}
-(void)textButtonClicked:(id)sender
{
NSLog(#"Hi");
[self.expandTextView setHidden:YES\];
NSLog(#"hey");
}
and how do I know which button is getting pressed.
You are doing it wrong. Your method should look like this :
-(IBAction)textButtonClicked:(id)sender
{
NSLog(#"Hi");
[self.expandTextView setHidden:YES];
NSLog(#"hey");
}
After writing this Method in your ViewContrller, wire up your Expand Button's -touchUpInside Method to -textButtonClicked Method. Use this line :
[self.expandButton addTarget:self action:#selector(textButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
instead the one in your code.
i have an iPhone app with TabBarController that each tab has a Nav Controller.. one of the Nav Controller pushes to another Viewcontroller. in that View Controller i am trying to Hide the back button and place One of my custom buttons. in the
- (void)viewDidLoad
{
[super viewDidLoad];
self._allUsersParticipantsInTheVideo = [[NSMutableArray alloc] init];
self._allUsersInvitesInTheVideo = [[NSMutableArray alloc] init];
for (int i=0; i < [self._curVideoToShow._videoParticipants count]; i++) {
[self._allUsersParticipantsInTheVideo addObject:[self._curVideoToShow._videoParticipants objectAtIndex:i]];
}
for (int i=0; i < [self._curVideoToShow._videoInvitees count]; i++) {
[self._allUsersInvitesInTheVideo addObject:[self._curVideoToShow._videoInvitees objectAtIndex:i]];
}
[self setNavBar];
}
#pragma mark - Set NavBar
-(void)setNavBar{
self.navigationItem.hidesBackButton = YES;
UIView *navBarItemview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navBarItemview setBackgroundColor:[UIColor clearColor]];
// Set cutsom back button
//UIImage *backImage = [UIImage imageNamed:#"backButton.png"];
UIImage *backImage = [UIImage imageNamed:#"LeftArrowBg.png"];
CGRect frame = CGRectMake(0, 6, backImage.size.width + 4, backImage.size.height);
UIButton *bttn = [[UIButton alloc] initWithFrame:frame];
[bttn setBackgroundImage:backImage forState:0];
[bttn setTag:kBackBttn];
[bttn setTitle:#"Back" forState:UIControlStateNormal];
[bttn setTitleEdgeInsets:UIEdgeInsetsMake(4, 5, 0, 0)];
[bttn.titleLabel setFont:[UIFont fontWithName:#"PTSans-Bold" size:12.0]];
[bttn setContentMode:UIViewContentModeScaleAspectFit];
[bttn addTarget:self action:#selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[navBarItemview addSubview:bttn];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, 320, 40)];
[titleLbl setFont:[UIFont fontWithName:#"PTSans-Bold" size:24.0]];
[titleLbl setTextColor:[UIColor whiteColor]];
[titleLbl setTextAlignment:UITextAlignmentCenter];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTag:kTitleLbl];
[titleLbl setShadowOffset:CGSizeMake(0, 1)];
[titleLbl setShadowColor:[UIColor blackColor]];
[titleLbl setText:#"All Participants"];
[navBarItemview addSubview:titleLbl];
self.navigationItem.titleView = navBarItemview;
}
As you can see i've written :
self.navigationItem.hidesBackButton = YES;
but on the first time when the app is lunched, i see the back button of the NavBar only for the first time, but later on for every time that i open the app the button remains to be my custom one. ( and also when i Navigate it is my custom )
someone?
try with this..
-(void)viewWillAppear:(BOOL)animated
{
self.navigationItem.hidesBackButton = YES;
}
or
[self.navigationController.navigationItem setHidesBackButton:YES];
or
self.navigationController.navigationItem.backBarButtonItem = nil;
I have created UIToolbar control.which is added as setInputAccessoryView to one UITextField Control.Which works fine. the tollbar comes up with uikeyboard when tapping UITextField.Up to this no issues.
When i placed one button, when tapping on it i want to hide the keyboard and only want to display the toolbar.
This is not happening.the toolbar is getting hidden.
Please let me know , what is the problem/ reason in the code.
Thanks for your time.
- (void)viewDidLoad {
[super viewDidLoad];
//
myToolbar = [[UIToolbar alloc] init];
[myToolbar sizeToFit];
myToolbar.frame = CGRectMake(0,198, 320, 35);
myToolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:myToolbar];
UIImage *image = [UIImage imageNamed:#"orangebuttonsmall.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 100, image.size.width, 25 );
aButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[aButton setTitle:#"Tap" forState:UIControlStateNormal];
[aButton setBackgroundImage: image forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
aButton.titleLabel.font = [UIFont boldSystemFontOfSize:10];
[aButton addTarget:self action:#selector(aCenterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
myToField = [[UITextField alloc] init];
myToField.frame = CGRectMake(0, 0, 320, 48);
myToField.textColor = [UIColor blackColor]; //text color
myToField.font = [UIFont systemFontOfSize:17.0]; //font size
myToField.placeholder = #"To:"; //place holder
myToField.backgroundColor = [UIColor whiteColor]; //background color
myToField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
myToField.keyboardType = UIKeyboardTypePhonePad; // type of the keyboard
myToField.returnKeyType = UIReturnKeyDone; // type of the return key
myToField.clearButtonMode = UITextFieldViewModeWhileEditing;
[myToField setInputAccessoryView:myToolbar];
myToField.textAlignment = UITextAlignmentLeft;
[myToField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
myToField.rightViewMode=UITextFieldViewModeAlways;
// has a clear 'x' button to the right
myToField.delegate = self;
[myToField becomeFirstResponder];
[self.view addSubview:myToField];
}
-(void)aCenterBtnClicked:(id)sender{
[myToField resignFirstResponder];
myToolbar.frame = CGRectMake(0,198, 320, 35);
myToolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:myToolbar];
}
inputAccessoryViews are placed as a subview on the UIKeyboard, if you hide the keyboard, it will hide the inputAccessoryView. Try adding the toolbar as a subview of self.view and animate it's position up when the keyboard is displayed by subscribing to the UIKeyboardWillShow notification and pulling the animation duration from the userInfo.
You try for static toolbar i think that will be easy for you.I am not done before remove from setinputview because there is no method for remove set input view.
I am customizing the UISegmentedControl, but I got a problem.
How to apply background image in the UISegmentedControl ? Changing the tint color does not fulfill my requirements.
Thanks
////Segmented Controll
NSArray *segmentTextContent = [NSArray arrayWithObjects: #"First",#"Second",#"Third",#"Forth", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.frame = CGRectMake(2, 5, 316, 35);
[segmentedControl addTarget:self action:#selector(segmentAction) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.enabled = true;
segmentedControl.selectedSegmentIndex = 0;
// cutomize the font size inside segmentedControl
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:11]];
//[label setTextColor:[UIColor greenColor]];
}
}
}
You could try http://idevrecipes.com/2010/12/11/custom-segmented-controls/.
problem in adding second UIbutton in cameraOverlayView ,here i am able to add the first button but not able to add second button with following code
- (void)pickAndDecodeFromSource:(UIImagePickerControllerSourceType) sourceType {
[self reset];
// Create the Image Picker
if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
UIImagePickerController* aPicker =
[[[UIImagePickerController alloc] init] autorelease];
aPicker.sourceType = sourceType;
aPicker.delegate = self;
self.picker = aPicker;
// [[NSUserDefaults standardUserDefaults] boolForKey:#"allowEditing"];
BOOL isCamera = (sourceType == UIImagePickerControllerSourceTypeCamera);
if ([picker respondsToSelector:#selector(setAllowsEditing:)]) {
// not in 3.0
[picker setAllowsEditing:!isCamera];
}
if (isCamera) {
if ([picker respondsToSelector:#selector(setShowsCameraControls:)]) {
[picker setShowsCameraControls:NO];
UIButton *cancelButton =
[UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString *cancelString =
NSLocalizedString(#"DecoderViewController cancel button title", #"");
CGFloat height = [UIFont systemFontSize];
CGSize size =
[cancelString sizeWithFont:[UIFont systemFontOfSize:height]];
[cancelButton setTitle:cancelString forState:UIControlStateNormal];
//cancelButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"CancelButtonForButton.png"]];
//cancelButton.backgroundColor = [UIColor clearColor];
//cancelButton.backgroundColor = [UIColor greenColor];
[cancelButton setImage:[UIImage imageNamed:#"cancelForButton.png"] forState:UIControlStateNormal];
//[cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
CGRect appFrame = [[UIScreen mainScreen] bounds];
static const int kMargin = 10;
static const int kInternalXMargin = 10;
static const int kInternalYMargin = 10;
CGRect frame = CGRectMake(kMargin,
appFrame.size.height - (height + 2*kInternalYMargin + kMargin),
2*kInternalXMargin + size.width,
height + 2*kInternalYMargin);
[cancelButton setFrame:frame];
[cancelButton addTarget:self
action:#selector(cancel:)
forControlEvents:UIControlEventTouchUpInside];
picker.cameraOverlayView = cancelButton;
// The camera takes quite a while to start up. Hence the 2 second delay.
[self performSelector:#selector(takeScreenshot)
withObject:nil
afterDelay:2.0];
//cancelButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg.png"]];
}
}
// Picker is displayed asynchronously.
[self presentModalViewController:picker animated:YES];
} else {
NSLog(#"Attempted to pick an image with illegal source type '%d'", sourceType);
}
}
The issue is that you are replacing the camera overlay with the first button - so creating the second button and using "picker.cameraOverlayView = newButton;" replaces the camera-overlay again.
The solution is to create a parent UIView, add both buttons to it, and then set the camera overlay to be the parent UIView.