ios - set / unset button background color - colors

Using the method:
- (IBAction)buttonClicked:(id)sender {
mybutton.backgroundColor = [UIColor blackColor];
}
I can set the black color to mybutton. How can I toggle between two color, each time button is clicked?
Tx in advance

In your h file initialize bool property
#property (nonatomic, assign) BOOL myBool;
In your m file put following line in viewDidLoad
self.myBool = YES;
Then in your IBAction,
- (IBAction)buttonClicked:(id)sender
{
if (self.myBool)
{
self.button.backgroundColor = [UIColor blackColor];
self.myBool = NO;
}
else
{
self.button.backgroundColor = [UIColor blackColor];
self.myBool = YES;
}
}

Related

replace background images with background color in all screen

I've facing one problem while developing my application.In my application there are more then 10 screens and have done with design too.But now i want to change whole color of the screen while user can open color picker and selected color will apply on all the screes.
Can any one suggest me that how can i do this one.because i have used lot's of imageview and images too.
Below is the example code where i have used the images in uitableview cell :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CategoriesListCell";
CategoriesListCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[CategoriesListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
[cell.contentView setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
int row = indexPath.row * 2;
NSLog(#"row:::%d",row);
UIButton *btnCategoriesLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, btnWidth, rowHeight)];
UIImage *imgButton = [UIImage imageNamed:#"bgbutton"];
[btnCategoriesLeft setImage:imgButton forState:UIControlStateNormal];
[btnCategoriesLeft addTarget:self action:#selector(btnCatTapped:) forControlEvents:UIControlEventTouchUpInside];
[btnCategoriesLeft setTag:row];
//Set Transpernt images
UIImageView *imgViewLeft = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 88, 53)];
[imgViewLeft setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",row]]];
[imgViewLeft setBackgroundColor:[UIColor clearColor]];
[btnCategoriesLeft addSubview:imgViewLeft];
//Set Title of label
UILabel *lblCategoriesTitleLeft = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 140, 20)];
lblCategoriesTitleLeft.backgroundColor = [UIColor clearColor];
lblCategoriesTitleLeft.textColor = [UIColor whiteColor];
[lblCategoriesTitleLeft setFont:FONT_WITH_SIZE_REGULAR(15.0)];
[lblCategoriesTitleLeft setText:[arrListOfCategories objectAtIndex:row]];
[lblCategoriesTitleLeft setTextAlignment:NSTextAlignmentCenter];
[btnCategoriesLeft addSubview:lblCategoriesTitleLeft];
[[cell contentView] addSubview:btnCategoriesLeft];
UIButton *btnCategoriesRight = [[UIButton alloc] initWithFrame:CGRectMake(btnWidth, 0, btnWidth, rowHeight)];
imgButton = [imgButton stretchableImageWithLeftCapWidth:imgButton.size.width/2 topCapHeight:imgButton.size.height/2];
[btnCategoriesRight setImage:imgButton forState:UIControlStateNormal];
[btnCategoriesRight setTag:row +1];
[btnCategoriesRight addTarget:self action:#selector(btnCatTapped:) forControlEvents:UIControlEventTouchUpInside];
//Set Transpernt images
UIImageView *imgViewRight = [[UIImageView alloc] initWithFrame:CGRectMake(40, 30, 88, 53)];
[imgViewRight setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",row+1]]];
[imgViewRight setBackgroundColor:[UIColor clearColor]];
[btnCategoriesRight addSubview:imgViewRight];
//Set Title of label
UILabel *lblCategoriesTitleRight = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 140, 20)];
lblCategoriesTitleRight.backgroundColor = [UIColor clearColor];
lblCategoriesTitleRight.textColor = [UIColor whiteColor];
[lblCategoriesTitleRight setFont:FONT_WITH_SIZE_REGULAR(15.0)];
[lblCategoriesTitleRight setText:[arrListOfCategories objectAtIndex:row+1]];
[lblCategoriesTitleRight setTextAlignment:NSTextAlignmentCenter];
[btnCategoriesRight addSubview:lblCategoriesTitleRight];
[[cell contentView] addSubview:btnCategoriesRight];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
Thanks.
I am a bit confused about how your sample code correlates to the question you're asking. If I understand this correctly, you would like to change the background color of all 10 or more screens with a slider. Try setting up red, blue, and green sliders and assigning global variables via the app delegate.
Add three sliders to interface builder.
Then set up three UISlider outlets.
YourSliderViewController.h
#interface YourViewController ()
#property (nonatomic, weak) IBOutlet UISlider *redSlider;
#property (nonatomic, weak) IBOutlet UISlider *blueSlider;
#property (nonatomic, weak) IBOutlet UISlider *greenSlider;
#end
YourSliderViewController.m
- (IBAction)changeColor:(id)sender{
float red = self.redSlider.value;
float green = self.greenSlider.value;
float blue = self.blueSlider.value;
UIColor *newColor = [UIColor colorWithRed:red
green:green
blue:blue
alpha:1.0];
self.view.backgroundColor = newColor;
}
The following is to make your RGB sliders accessible to all classes. That way you can set self.view.background = newColor in any other ViewController inside -viewDidLoad.
Create a pointer to the AppDelegate. I called mine appDel.
MyXcodeProject-Prefix.pch
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
AppDelegate *appDel;
#endif
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIColor *newColor;
#end
AppDelegate.m
appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];

UITextView to Mimic UITextField [for basic round corner text field]

I want to have UITextField with multiple lines, after a quick google on this issue I found that I should use TextView so I did switch my code to use UITextView when I want multiple lines. My View still have other one line textField that I want to keep.
To make my TextView looks like TextField, I had to add code to set border and radius, but they look a little bit different on iOS7. Does anyone know:
what is the color for the UITextField border? when both enabled and disabled so I can sent my textview to match it.
what is radius of the corner of TextField.
what is the background color for UITextField when it is disabled[attached picture shows the text field has lighter shade of grey when it is disabled]? so i can set my text view to the same color when i disable user interaction.
If there is away to keep using textfield for multiline text, I am all ears and i switch to use it.
Best Regards,
I use this:
textView.layer.borderColor = [[UIColor colorWithRed:215.0 / 255.0 green:215.0 / 255.0 blue:215.0 / 255.0 alpha:1] CGColor];
textView.layer.borderWidth = 0.6f;
textView.layer.cornerRadius = 6.0f;
little differences in the params make it looks more like UITextField(I hope).
I use this:
#import <QuartzCore/QuartzCore.h>
-(void) textViewLikeTextField:(UITextView*)textView
{
[textView.layer setBorderColor:[[UIColor colorWithRed:212.0/255.0
green:212.0/255.0
blue:212.0/255.0
alpha:1] CGColor]];
[textView.layer setBorderWidth:1.0f];
[textView.layer setCornerRadius:7.0f];
[textView.layer setMasksToBounds:YES];
}
and get a good resut.
I have a small subclass of UITextView that gives in iOS 7 the same look as the UITextField
The interface is empty:
#interface MyTextView : UITextView
#end
The implementation overwrites the initialization and the setter of the 'editable' property:
#implementation MyTextView
//================================================================================
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame:frame];
if (self)
{
[self commonInit];
}
return self;
}
//================================================================================
- (id) initWithCoder: (NSCoder *) aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self commonInit];
}
return self;
}
//================================================================================
- (void) commonInit
{
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [[UIColor colorWithRed:232.0/255.0
green:232.0/255.0 blue:232.0/255.0 alpha:1] CGColor];
self.layer.cornerRadius = 6;
}
//================================================================================
- (void) setEditable: (BOOL) editable
{
[super setEditable:editable];
if (editable)
{
self.backgroundColor = [UIColor whiteColor];
}
else
{
self.backgroundColor = [UIColor colorWithRed:250.0/255.0
green:250.0/255.0 blue:250.0/255.0 alpha:1];
}
}
//================================================================================
#end
This is the closest I got with enabled UITextView
[yourTextView.layer setBorderColor:[[[UIColor lightGrayColor] colorWithAlphaComponent:0.2] CGColor]];
[yourTextView.layer setBorderWidth:2.0];
yourTextView.layer.cornerRadius = 5;
yourTextView.clipsToBounds = YES;
yourTextView.textColor = [UIColor lightGrayColor];

MKpolyline not visible at MKMapKit

I have a problem with my MKPolyline.
I have added this code but I see no line. I get the coordinates form a webserver with a http-request. I save the coordinates as a double and show with that my annotations.
in the viewcontroller.h
#property (nonatomic, retain) MKPolyline *routeLine;
#property (nonatomic, retain) MKPolylineView *routeLineView;
in the viewcontroller.m
CLLocationCoordinate2D coordinateArray[7];
coordinateArray[0] = CLLocationCoordinate2DMake(theCoordinate0.latitude, theCoordinate0.longitude);
coordinateArray[1] = CLLocationCoordinate2DMake(theCoordinate1.latitude, theCoordinate1.longitude);
coordinateArray[2] = CLLocationCoordinate2DMake(theCoordinate2.latitude, theCoordinate2.longitude);
coordinateArray[3] = CLLocationCoordinate2DMake(theCoordinate3.latitude, theCoordinate3.longitude);
coordinateArray[4] = CLLocationCoordinate2DMake(theCoordinate4.latitude, theCoordinate4.longitude);
coordinateArray[5] = CLLocationCoordinate2DMake(theCoordinate5.latitude, theCoordinate5.longitude);
coordinateArray[6] = CLLocationCoordinate2DMake(theCoordinate6.latitude, theCoordinate6.longitude);
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:7];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible
[self.mapView addOverlay:self.routeLine];
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if(overlay == self.routeLine)
{
if(nil == self.routeLineView)
{
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine]autorelease];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 10;
}
return self.routeLineView;
}
return nil;
}
Have you verified that your mapview is connected to the mapview on screen (the IBOutlet in Interface Builder)? the next thing to check is that you've set the delegate. Check this by putting a breakpoint or a debug statement in the viewForOverlay function.
P.S. with that code you're only ever going to be able to draw one line. It relies on self.routeLine and self.routeLineView and you only have one of each of those.

Custom MKAnnotaionView, Display title/subtitle Position on Map

1) My annotation shows the title and subtitle of not.
I have already tried a lot, but I can not.
2) If an annotation has been selected, the annotation will be centered in the middle of the window. So the map is moved. Any idea?
3) My Callout button does not work anymore, I've got the # selector (openAnything) but I want to use this function is triggered - (void) mapView: (MKMapView *) mapView annotationView: (MKAnnotationView *) view calloutAccessoryControlTapped: (UIControl *) control {
Enough to ask, here's a video and some source code
http://www.youtube.com/watch?v=Ur1aqeYEFHw&feature=youtube_gdata_player
Thanks
TAPPMapViewControler.m
- (MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[TAPPMapAnnotation class]])
{
static NSString *shopAnnotationIdentifier = #"Filiale";
//pinView = (MKPinAnnotationView *)
TAPPMapAnnotationCustom* pinView = (TAPPMapAnnotationCustom*)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotationIdentifier];
[self.myMapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotationIdentifier];
if (pinView == nil)
{
// if an existing pin view was not available, create one
TAPPMapAnnotationCustom *customPinView = [[TAPPMapAnnotationCustom alloc] initWithAnnotation:annotation reuseIdentifier:shopAnnotationIdentifier];
customPinView.image = [UIImage imageNamed:#"map_pin.png"];
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
TAPPMapAnnotation *theAnnotation = view.annotation;
TAPPMapAnnotationDetail *theController = [self.storyboard instantiateViewControllerWithIdentifier:#"MapAnnotationDetail"];
theController.theAnnotationId = theAnnotation.objectId;
[self.navigationController pushViewController:theController animated:YES];
}
TAPPMapAnnotationCustom.h
#import <MapKit/MapKit.h>
#interface TAPPMapAnnotationCustom : MKAnnotationView
#property (strong, nonatomic) UIImageView *calloutView;
#property (strong, nonatomic) UILabel *title;
#property (strong, nonatomic) UILabel *subTitle;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
- (void)animateCalloutAppearance:(BOOL)inAdd;
#end
TAPPMapAnnotationCustom.m
#import "TAPPMapAnnotationCustom.h"
#import "TAPPMapAnnotation.h"
#implementation TAPPMapAnnotationCustom
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
[super setSelected:selected animated:animated];
if(selected)
{
// Remove Image, because we set a second large one.
self.image = Nil;
UIImage *imageBack = [[UIImage imageNamed:#"map_pin.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 35, 0, 6)];
self.calloutView = [[UIImageView alloc]initWithImage:imageBack];
[self.calloutView setFrame:CGRectMake(0,0,0,0)];
[self.calloutView sizeToFit];
[self addSubview:self.calloutView ];
// Callout Button
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(openAnything) forControlEvents:UIControlEventTouchUpInside];
rightButton.frame = CGRectMake(0
, ((self.calloutView.frame.size.height-6) / 2)-(rightButton.frame.size.height / 2)
, rightButton.frame.size.width
, rightButton.frame.size.height);
self.rightCalloutAccessoryView = rightButton;
self.rightCalloutAccessoryView.hidden = YES;
self.rightCalloutAccessoryView.alpha = 0;
[self addSubview:self.rightCalloutAccessoryView];
// Start Annimation
[self animateCalloutAppearance:YES];
} else {
//Start Annimation and Remove from view
[self animateCalloutAppearance:NO];
}
}
- (void)didAddSubview:(UIView *)subview{
if ([[[subview class] description] isEqualToString:#"UICalloutView"]) {
for (UIView *subsubView in subview.subviews) {
if ([subsubView class] == [UIImageView class]) {
UIImageView *imageView = ((UIImageView *)subsubView);
[imageView removeFromSuperview];
}else if ([subsubView class] == [UILabel class]) {
UILabel *labelView = ((UILabel *)subsubView);
[labelView removeFromSuperview];
}
}
}
}
- (void)animateCalloutAppearance:(BOOL)inAdd {
if (inAdd == YES) {
self.rightCalloutAccessoryView.hidden = NO;
[UIView animateWithDuration:0.4 delay:0 options: UIViewAnimationOptionTransitionNone
animations:^{
self.calloutView.frame = CGRectMake(self.calloutView.frame.origin.x
, self.calloutView.frame.origin.y
, self.calloutView.frame.size.width+150
, self.calloutView.frame.size.height);
self.rightCalloutAccessoryView.alpha = 1;
self.rightCalloutAccessoryView.frame = CGRectMake(self.calloutView.frame.size.width - (self.rightCalloutAccessoryView.frame.size.width)
, self.rightCalloutAccessoryView.frame.origin.y
, self.rightCalloutAccessoryView.frame.size.width
, self.rightCalloutAccessoryView.frame.size.height);
} completion:^(BOOL finished){ }];
} else {
[UIView animateWithDuration:0.4 delay:0 options: UIViewAnimationOptionTransitionNone
animations:^{
self.rightCalloutAccessoryView.alpha = 0;
self.rightCalloutAccessoryView.frame = CGRectMake(0
, self.rightCalloutAccessoryView.frame.origin.y
, self.rightCalloutAccessoryView.frame.size.width
, self.rightCalloutAccessoryView.frame.size.height);
self.calloutView.frame = CGRectMake(self.calloutView.frame.origin.x
, self.calloutView.frame.origin.y
, self.calloutView.frame.size.width-150
, self.calloutView.frame.size.height);
} completion:^(BOOL finished){
self.image = [UIImage imageNamed:#"map_pin.png"];
[self.calloutView removeFromSuperview];
[self.rightCalloutAccessoryView removeFromSuperview];
}];
}
}
#end
Your annotation class needs to have a subtitle attribute, yours has subTitle.
If calloutAccessoryControlTapped is not being called it is usually a sign your MKMapview's delegate has not been set. However you're only setting the accessory button when it gets selected. Do you have a good reason for that? Since the callout window won't be visible until it is selected, I would advise you to set the accessory button in viewForAnnotation and let it appear when the callout window does.
I fix the first one:
TAPPMapAnnotation *theAnnotation = (TAPPMapAnnotation *)self.annotation;
self.title = [[UILabel alloc]init];
self.title.font = [UIFont systemFontOfSize:12.0];
self.title.textColor = [UIColor whiteColor];
self.title.backgroundColor = [UIColor clearColor];
self.title.text = theAnnotation.title;
[...]
[self.theView addSubview:self.title];

Creating a stroke and shadow affect using only 1 UILabel subclass

Im trying to create a UILabel subclass that will accomplish the task of the 2 separate UILabels i have stacked on top of each other using the following methods:
Background UILabel:
- (void)drawTextInRect:(CGRect)rect {
CGSize shadowOffset = self.shadowOffset;
//UIColor *textColor = self.textColor;
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 3);
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = [UIColor whiteColor];
[super drawTextInRect:rect];
self.alpha = .1;
self.shadowOffset = shadowOffset;
}
Foreground UILabel:
- (void)drawTextInRect:(CGRect)rect {
CGSize shadowOffset = self.shadowOffset;
UIColor *textColor = self.textColor;
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 2);
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = [UIColor blackColor];
[super drawTextInRect:rect];
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
self.alpha = .5;
self.shadowOffset = shadowOffset;
}
Is there a way to accomplish these 2 sets of affects using only 1 UILabel subclass?
Try this code:
UILabel* label = [[UILabel alloc] init];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(0,1);

Resources