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.
Related
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];
}
}
I created a custom button as shown in below:
-(void)addYellowBtn{
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setBackgroundImage:[UIImage imageNamed: #"yellow3.jpg"]forState:UIControlStateNormal];
//sets background image for highlighted state
[customButton setBackgroundImage:[UIImage imageNamed: #"yellow5.jpg"] forState:UIControlStateHighlighted];
[customButton setFrame:CGRectMake(60, 100, 60, 60)];
[customButton setTitle:#"" forState:UIControlStateNormal];
CALayer *btnLayer = [customButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:12.0f];
[self.view addSubview:customButton];
[customButton addTarget:self action:(btnclick1:) forControlEvents:UIControlStateNormal];
}
when i click this button, next view will have to push.To do this,how can i write codings?
Change this:
[customButton addTarget:self action:(btnclick1:) forControlEvents:UIControlStateNormal];
to this
[customButton addTarget:self action:(btnclick1:) forControlEvents: UIControlEventTouchUpInside];
UIControlStateNormal is not a touch event.
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]
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.
I am creating buttons programmatically. I want to change button background color while touching up inside again it has to set back to its usual color after lifting up our finger.....
nine = [UIButton buttonWithType:UIButtonTypeCustom];
[nine setFrame:CGRectMake(15, 105, 65, 40)];
[nine setTitle:#"9" forState:UIControlStateNormal];
[nine setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[nine setBackgroundColor:[UIColor cyanColor]];
[nine addTarget:self action:#selector(clickDigit:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nine];
// to change background color
-(void)changeButtonBackGroundColor:(id) sender
{
[nine setBackgroundColor:[UIColor redColor]];
}
Here changeBackgroundColor method was created to change color of that button . it changes color.
Don't know if this relates to your question but:
this
[nine setBackgroundColor:[UIColor redColor]];
should be
[sender setBackgroundColor:[UIColor redColor]];
Edit:
Change this
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
to
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchDown];
[nine addTarget:self action:#selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:#selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside];
[nine addTarget:self action:#selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchCancel];
and add the method:
- (void)resetButtonBackGroundColor: (UIButton*)sender {
[sender setBackgroundColor:[UIColor cyanColor]];
}
I think you have two options..
First:
You can put the [nine setBackgroundColor:[UIColor redColor]]; inside "clickDigit" (and like dasdom said rename to sender and change to (UIButton*)sender)..
Change
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
to
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside];
and the method "changeButtonBackGroundColor"
[sender setBackgroundColor:[UIColor cyanColor]];
Second
create universal UIControlEvents
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventAllEvents];
-(void)changeButtonBackGroundColor:(UIButton*) sender{
if ([sender.backgroundColor isEqual:[UIColor redColor]]){
[sender setBackgroundColor:[UIColor cyanColor]];
}else{
[sender setBackgroundColor:[UIColor redColor]];
}}
I didn't try this code