UIButton setTitle Colour Changing not working - objective-c

I have created scroll view programmatically and created N number of buttons using for loop, but now I need to change setTitle color accordingly the button selected or unselected
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(40,50,self.view.frame.size.width,40)];
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.showsHorizontalScrollIndicator = NO;
for (int i = 0; i<_pagesNameArray.count; i++) {
self.button = [[UIButton alloc]init];
self.button.frame = CGRectMake(i*150, 0, 150, 40);
self.button.tag = i;
self.button.backgroundColor = [UIColor clearColor];
[self.button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:[_pagesNameArray objectAtIndex:i] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[_scrollView addSubview:self.button];
}
[self setupSelector];
CGRect contentRect = CGRectZero;
for (UIView *view in _scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
_scrollView.contentSize = contentRect.size;
[self.view addSubview:_scrollView];
I also tried this method but, it's not working. Can anyone help me out?
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

-(void)tapSegmentButtonAction:(UIbutton *)sender
{
UIbutton *button=sender.tag;
[self.button setTitleColor:[UIColor blackColor]
forState:UIControlStateSelected];
}

I have created 3 different type of buttons and given tag to each button
self.button = [[UIButton alloc]init];
self.button.frame = CGRectMake(0, 0, 180, 30);
self.button.tag = 0;
self.button.backgroundColor = [UIColor clearColor];
[self.button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:#"LOCAL REPORTS" forState:UIControlStateNormal];
// [self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
self.button.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.button];
self.buttonNews = [[UIButton alloc]init];
self.buttonNews.frame = CGRectMake(135, 0, 180, 30);
self.buttonNews.tag = 1;
self.buttonNews.backgroundColor = [UIColor clearColor];
[self.buttonNews addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonNews setTitle:#"NEWS" forState:UIControlStateNormal];
//[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.buttonNews.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
self.buttonNews.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.buttonNews];
self.buttonTop = [[UIButton alloc]init];
self.buttonTop.frame = CGRectMake(135*2, 0, 180, 30);
self.buttonTop.tag = 2;
self.buttonTop.backgroundColor = [UIColor clearColor];
[self.buttonTop addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonTop setTitle:#"TOP PHOTOS" forState:UIControlStateNormal];
//[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.buttonTop.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
self.buttonTop.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.buttonTop];
[self animateButton];
-(void) animateButton
{
[self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
if (self.currentIndex == 0) {
[self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
if (self.currentIndex == 1) {
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
if (self.currentIndex == 2) {
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
}
}

Related

Programatically created UIDatepicke done and cancle button is not working

This is my UIDatePicker code but in that picker, done and cancle button is not working ...please help me.... i also add here my done and cancle button methods here..Please give me a proper suggessions for how it works properly.
if (textField.tag==Jobdatetxt.tag)
{
[Jobdatetxt resignFirstResponder];
[data.toolbar removeFromSuperview];
UITextField *lagFreeField = [[UITextField alloc] init];
[self.view.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
//////////////////////////////////////////////// DATE OF JOB////////////////////////////////////////////////
pickerDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,result.height - 216, result.width, 216)];
[pickerDate setDatePickerMode:UIDatePickerModeDate];
pickerDate.backgroundColor = [UIColor whiteColor];
[self.view.window addSubview:pickerDate];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.frame=CGRectMake(0,result.height - 266,result.width,50);
cancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancel setTitle:#"Cancel" forState:UIControlStateNormal];
cancel.backgroundColor = [UIColor clearColor];
[cancel.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
cancel.titleLabel.textColor = [UIColor whiteColor];
[cancel addTarget:self action:#selector(CancelClicked:) forControlEvents:UIControlEventTouchUpInside];
cancel.tag = textField.tag;
cancel.frame = CGRectMake(10, 10, 70, 30);
[toolbar addSubview:cancel];
doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.backgroundColor = [UIColor clearColor];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
doneButton.titleLabel.textColor = [UIColor whiteColor];
[doneButton addTarget:self action:#selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
doneButton.tag = textField.tag;
doneButton.frame = CGRectMake(result.width - 80, 10, 70, 30);
[toolbar addSubview:doneButton];
[self.view.window addSubview:toolbar];
}
// Done Button method
-(IBAction)DoneClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}
//Cancle Button Method
-(IBAction)CancelClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}

On click of a button a textfield has to be displayed?

I have a button and a textfield. Button is just below the textField. Now when I click on the button a new textfield should be displayed below the first text field and the frame of the button should also be changed. When I have tried this it worked for the first time but when I click a second time the new textfield is not displaying and the button frame is also not changing. Help me with this issue.
The code that i have written is:
-(void)onboardCertificatesAddBtnBtnTap
{
int x, y;
if (y =585, x=632) {
_boardCertificationField= [[UITextField alloc]initWithFrame:CGRectMake(5, y+50, self.scrollView.frame.size.width/2-25, 40)];
_boardCertificationField.font = [UIFont systemFontOfSize:17];
_boardCertificationField.borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_boardCertificationField];
_expDateField = [[UITextField alloc]initWithFrame:CGRectMake(220, y+50, self.scrollView.frame.size.width/2-40, 40)];
_expDateField .font = [UIFont systemFontOfSize:17];
_expDateField .borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_expDateField];
_boardCertificatesAddBtn.frame = CGRectMake(5, x+50, 20, 20);
[_boardCertificatesAddBtn setImage:[UIImage imageNamed:#"Add1.png"] forState:UIControlStateNormal];
[_scrollView addSubview:_boardCertificatesAddBtn];
[_boardCertificatesAddBtn addTarget:self action:#selector(onboardCertificatesAddBtnBtnTap) forControlEvents:UIControlEventTouchUpInside];
y=y+50;
x=x+50;
}
else {
_boardCertificationField= [[UITextField alloc]initWithFrame:CGRectMake(5, y+50, self.scrollView.frame.size.width/2-25, 40)];
_boardCertificationField.font = [UIFont systemFontOfSize:17];
_boardCertificationField.borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_boardCertificationField];
_expDateField = [[UITextField alloc]initWithFrame:CGRectMake(220, y+50, self.scrollView.frame.size.width/2-40, 40)];
_expDateField .font = [UIFont systemFontOfSize:17];
_expDateField .borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_expDateField];
_boardCertificatesAddBtn.frame = CGRectMake(5, x+50, 20, 20);
[_boardCertificatesAddBtn setImage:[UIImage imageNamed:#"Add1.png"] forState:UIControlStateNormal];
[_scrollView addSubview:_boardCertificatesAddBtn];
[_boardCertificatesAddBtn addTarget:self action:#selector(onboardCertificatesAddBtnBtnTap) forControlEvents:UIControlEventTouchUpInside];
y=y+50;
x=x+50;
}
}
Declare x and in #implementation ViewController block
like
#implementation Viewcontroller
{
int x,y;
}
and write other code as
-(void)onboardCertificatesAddBtnBtnTap
{
if (y =585, x=632) {
_boardCertificationField= [[UITextField alloc]initWithFrame:CGRectMake(5, y+50, self.scrollView.frame.size.width/2-25, 40)];
_boardCertificationField.font = [UIFont systemFontOfSize:17];
_boardCertificationField.borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_boardCertificationField];
_expDateField = [[UITextField alloc]initWithFrame:CGRectMake(220, y+50, self.scrollView.frame.size.width/2-40, 40)];
_expDateField .font = [UIFont systemFontOfSize:17];
_expDateField .borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_expDateField];
_boardCertificatesAddBtn.frame = CGRectMake(5, x+50, 20, 20);
[_boardCertificatesAddBtn setImage:[UIImage imageNamed:#"Add1.png"] forState:UIControlStateNormal];
[_scrollView addSubview:_boardCertificatesAddBtn];
[_boardCertificatesAddBtn addTarget:self action:#selector(onboardCertificatesAddBtnBtnTap) forControlEvents:UIControlEventTouchUpInside];
y=y+50;
x=x+50;
}
else {
_boardCertificationField= [[UITextField alloc]initWithFrame:CGRectMake(5, y+50, self.scrollView.frame.size.width/2-25, 40)];
_boardCertificationField.font = [UIFont systemFontOfSize:17];
_boardCertificationField.borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_boardCertificationField];
_expDateField = [[UITextField alloc]initWithFrame:CGRectMake(220, y+50, self.scrollView.frame.size.width/2-40, 40)];
_expDateField .font = [UIFont systemFontOfSize:17];
_expDateField .borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_expDateField];
_boardCertificatesAddBtn.frame = CGRectMake(5, x+50, 20, 20);
[_boardCertificatesAddBtn setImage:[UIImage imageNamed:#"Add1.png"] forState:UIControlStateNormal];
[_scrollView addSubview:_boardCertificatesAddBtn];
[_boardCertificatesAddBtn addTarget:self action:#selector(onboardCertificatesAddBtnBtnTap) forControlEvents:UIControlEventTouchUpInside];
y=y+50;
x=x+50;
}

create hard custom button

I can not figure out how to construct this button. I need help in the creation of this, note that the strip is not painted when the button is active. What elements should I use?
on link you can look this button
http://i.stack.imgur.com/0mmJ0.png
UIButton *clone = [[UIButton alloc] initWithFrame:frame];
[clone setTitle:strDate forState:UIControlStateNormal];
[clone setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
clone.frame = CGRectMake(19, post_offer_top*(i-1)+22, 281, 75);
clone.tag = my_id;
// [clone setTitle:[[buttons objectAtIndex:i-1] objectForKey:#"title"] forState:UIControlStateNormal];
clone.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
clone.contentEdgeInsets = UIEdgeInsetsMake(44, 31, 0, 0);
[clone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clone setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[clone.titleLabel setFont:[UIFont fontWithName:#"PF DinDisplay Pro" size:10]];
[clone.titleLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
any ideas?
You should initialize buttons like this [UIButton buttonWithType:UIButtonTypeCustom]

UIButton to show BackgroundImage, Image and Title

I want to create a UIButton that uses its setBackgroundImage, setImage, and setTitle properties, but when I add setTitle in combination with setImage and setBackgroundImage the title doesn't appear.
I've tried the following.
[button setBackgroundImage:[UIImage imageNamed:#"image3.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"month_pct_general.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%d",[button tag]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
[button setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 25)];
your title on button and on title your all image it there so add label on the image like this.
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(6, 5, 60, 20)];
[Label setText:#"label"];
Label.backgroundColor=[UIColor clearColor];
[Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
Label.textColor=[UIColor whiteColor];
[myButton addSubview:Label];
your all image above the title only so your title is in background so use label on image view.
If you are making GUI using interface builder, then click on the label and go to Editor-> Arrange -> Send to Front.

placing image on table view iphone

I have three rows which are configured programmatically. I want to place an image only on the first row of the table view. The image has to cover the entire first row(not on the left side of table view cell by default)
Here is my code:
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case 1:
if(indexPath.row==1)
{
return 230.0; // first row is 123px high
}
default:
return 100.0; // all other rows are 40px high
}
}
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 0:
break;
case 1:
if (indexPath.row==1) {
UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(5, 10, 310, 40)];
[button1 setTitle:#"Give" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(give)
forControlEvents:UIControlEventTouchUpInside];
[button1.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button1];
UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(5, 60, 310, 40)];
[button2 setTitle:#"YouTube" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(Announcements)
forControlEvents:UIControlEventTouchUpInside];
[button2.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button2];
UIButton *button3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 setFrame:CGRectMake(5, 110, 310, 40)];
[button3 setTitle:#"Follow us on Twitter" forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(Twitter)
forControlEvents:UIControlEventTouchUpInside];
[button3.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button3];
UIButton *button4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 setFrame:CGRectMake(5, 160, 310, 40)];
[button4 setTitle:#"Follow us on FaceBook" forState:UIControlStateNormal];
[button4 addTarget:self action:#selector(Facebook)
forControlEvents:UIControlEventTouchUpInside];
[button4.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button4];
/*[button1 release];
[button2 release];
[button3 release];
[button4 release];*/
}
break;
default:
break;
}
}
You can add a UIImageView to your contentview.