In my app one UITableView is there. In table cell am placing two labels and one button. button height is equal to (label1+ label2) height. these are to display content. and button is for action. Here my problem is am not able to perform button action in all portion. only some part is working when clicking.
Here is the code am used to place the labels and buttons
artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)];
artistButton.backgroundColor = [UIColor redColor];
[self.contentView bringSubviewToFront:artistButton];
[self.contentView addSubview:artistButton];
artistLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 27.0)];
artistLabel1.textAlignment = UITextAlignmentLeft;
artistLabel1.textColor = [UIColor colorWithRed:0.14 green:0.29 blue:0.42 alpha:1];
artistLabel1.font = [UIFont boldSystemFontOfSize:15.0];
artistLabel1.backgroundColor = [UIColor blueColor];
[self.contentView addSubview:artistLabel1];
artistLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(11.0,20.0, 170.0, 27.0)];
artistLabel2.textAlignment = UITextAlignmentLeft;
artistLabel2.textColor = [UIColor grayColor];
artistLabel2.font = [UIFont boldSystemFontOfSize:12.0];
artistLabel2.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:artistLabel2];
any one can help or suggest.
Thanks in advance.
I found answer for my question.
Because of size of the button and labels only it is not working.
Now am adjusted the width and length of button and labels its working fine.
Thank you all.
add one line before UIButton declaration..
artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
after that you can initialize your UIButton code.
Add Target action to this Button
artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)];
artistButton.backgroundColor = [UIColor redColor];
[artistButton addTarget:self
action:#selector(ArtistAction)
forControlEvents:UIControlEventTouchUpInSide];
[self.contentView bringSubviewToFront:artistButton];
[self.contentView addSubview:artistButton];
-(void)ArtistAction
{
NSLog("Test Artist Action");
}
I suppose you do realize that the frames you set are overlapping.
You do call [self.contentView bringSubviewToFront:artistButton]; but you add the button as a subview after so it will have no effect.
Also you add other subviews after adding the button and those will be placed above you button.
So just add the two labels first and then the button you should be able to click the button properly, though you labels will be under the button and not sure how much of them will be visible. Check the frames you are setting for the objects you are adding.
Just use for your cell tableView:didSelectRowAtIndex: method.
Related
I need to align the navigationItem TitleView to the far right. (where the navigationItem rightBarButtonItem normally appears)
How can this be done ?
I have tried this but with no luck it still places TitleView in the center:
CGRect titleViewPos = CGRectMake(300, 0, 200, 10);
self.navigationItem.titleView.frame = titleViewPos;
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
lbl.textAlignment = UITextAlignmentRight;
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"TITLEVIEW";
self.navigationItem.titleView = lbl;
You will get output as
Just an idea:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(300, 0, 200, 10);
[btn setTitle:#"titleView" forState:UIControlStateNormal];
self.navigationItem.titleView = btn;
You need to pass a UIElement (UIView,UILabel) to navigation item then you can set the position and make other customisation with navigation title or left button or right button.
May this will help you.
try this one, May be use-full
UILabel *lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = NSTextAlignmentRight;
lbNavTitle.backgroundColor = [UIColor clearColor];
lbNavTitle.text = #"hello World";
self.navigationItem.titleView = lbNavTitle;
The titleview frame is automatically adjusted, if you want to obtain this result you can try this:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:self.navigationController.navigationBar.frame];
[titleLabel setTextAlignment:UITextAlignmentRight];
[titleLabel setText:#"Your title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
self.navigationItem.titleView = titleLabel;
NOTE: be sure to don't set self.title of your view controller otherwise you will overwrite the titleView
Another solution could be put the titleLabel into an UIBarButtonItem (customView) and set it like to self.navigationItem.rightBarButtonItem in this way you don't need to set the text alignement ad the frame of the label can be the proper text size
none of that worked for me. I took Desdanova's advice and just added as a subview. Makes sense because that's all it is! Initialize it with any frame position you want and then
[self.view addSubView:lbl];
and Voila!
I have made an iPad application, in that I used navigation control,
now in the title bar , I want to put image on left side, so I hide title bar with label, but label is not covering entire width of title bar,
IL APP IN THE SCREEN SHOT,
here is the code snippet,
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 800, 50)];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
titleView.backgroundColor=[UIColor blackColor];
titleView.text = #"IL APP";
titleView.textAlignment=UITextAlignmentCenter;
//self.navigationItem.titleView = titleView;
[self.navigationItem setTitleView:titleView];
[titleView release];
}
#Gama as far as your question is concerned you are asking to put an Image but your real issue being described is that the label is not covering up the title bar. To cover that properly you can use
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 32)];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
//titleView.backgroundColor=[UIColor blackColor];
titleView.text = #"IL APP";
titleView.textAlignment=UITextAlignmentCenter;
[self.navigationItem setTitleView:titleView];
[titleView release];
}
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
For using an image you can use an image view instead of a label to be assigned as the titleView for navigationItem.
Hope it helps
I tried to solved your problem but I get the same result.
But you can hide navigation bar and add the UILabel in to your ViewController
self.navigationController.navigationBarHidden = FALSE;
[self.view addSubview:titleview];
UIImageView *imageNav = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"navImage.png"]];
[self.navigationController.navigationBar addSubview:imagenav];
[self.navigationController.navigationBar sendSubviewToBack:imageNav];
[imageNav release];
Hi i wanted to do the same thing and i saw your posting, i basically used some of the code you provided and modified it a little and got it to work see below:
//declare and initialize your imageView
UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView;
titleImage = [[UIImageView alloc]
initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2)
- (100/2), 0, 100, self.navigationController.navigationBar.frame.size.height)];
//setting the image for UIImageView
titleImage.image = [UIImage imageName:#"photo.jpg"];
//NOTE: you have to do this line at the bottom to add image to the navigation bar Try it it WORKS!
self.navigationItem.titleView = titleImage;
I am working on a todo list application. It's however i cant seems to align my checkbox image properly with the cell.textLabel.text.
My problem for the code below is the words "test test" got cover by my checkbox image, which only left with "st test"
My code for the checkbox button image like
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5.0, 7.0, 25.0, 25.0)];
UIImage *uncheckedButtonImage = [UIImage imageNamed:#"unchecked_checkbox.png"];
[button setImage:uncheckedButtonImage forState:UIControlStateNormal];
[cell.contentView addSubview:button];
and my cell textlabel is just
cell.textLabel.text = #"test test";
Is there anyway the align the textLabel text to move a bit to the right?
I have suggested to Add UILabel because in the future if you needed to change the view cell it will be more Dynamic and easier to play around anything new to the Cell.
take a look to the following code snippet:
CGRect DeviceNameFrame = CGRectMake(101, 32, 522, 21);
UILabel *lblDeviceName = [[UILabel alloc] initWithFrame:DeviceNameFrame];
lblDeviceName.backgroundColor = [UIColor clearColor];
lblDeviceName.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
lblDeviceName.text = #"test test";
lblDeviceName.lineBreakMode = UILineBreakModeMiddleTruncation;
[cell.contentView addSubview:lblDeviceName];
[lblDeviceName release];
I hope this will help you.
The simplest solution would be to look at the "Indentation" properties of UITableViewCell. See the documentation for more info. Subclassing the cell and adding your own UILabel would give you the most control though.
Apple has many big colored buttons, that are not standard. One of them is a delete contact button in address book. Another is Start/Stop button in Timer (Clock application) or End Call button in Phone. They are different, but all have similar appearance.
Question is simple. Is there a way to create these kind of buttons without using background images/screenshots or recreating them from scratch?
Thanks.
You could try Opacity. The new release allows you to draw vector images and generate UIKit-friendly Objective-C code that recreates the drawing.
Well, I've tried many ways to do it, but the most simple was to make a UIButton with a custom style.
If you need a "delete" button in the footer of the table - like in contact or event, here is the code:
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UIButton *newDeleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
newDeleteButton.frame = CGRectMake(10, 10, 300, 43);
UIImage *buttonBackground = [UIImage imageNamed:#"ButtonDelete.png"];
[newDeleteButton setImage:buttonBackground forState:UIControlStateNormal];
[newDeleteButton addTarget:self action:#selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UILabel *deleteTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 39)];
deleteTitle.text = #"Delete";
deleteTitle.font = [UIFont boldSystemFontOfSize:20];
deleteTitle.textColor = [UIColor whiteColor];
deleteTitle.textAlignment = UITextAlignmentCenter;
deleteTitle.backgroundColor = [UIColor clearColor];
[footerView addSubview:newDeleteButton];
[footerView addSubview:deleteTitle];
formTableView.tableFooterView = footerView;
First, you create the view for footer.
Then you make a button with the correct background — just make a screenshot of any button you need and delete letters from it.
Than you create a caption for your button and place it over the button.
The last step is to put the button and caption in the footer view, and put the footer view into the table.
I have a simple navigation based application for the iphone/objective-c
within various UIViewControllers that are pushed into view, I can set the text in the title bar using something like
self.title = #"blah blah blah"
Is there a way to control the font and font-size of the title in the title bar text?
thanks!
the proper way to resize the title text of a navcontroller is to set the titleView property of the navigationItem
like this (in viewDidLoad)
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
You may want to emboss the label so it doesn't look fuzzy and flat:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:18.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.navigationItem.title;
// emboss so that the label looks OK
[label setShadowColor:[UIColor darkGrayColor]];
[label setShadowOffset:CGSizeMake(0, -0.5)];
self.navigationItem.titleView = label;
}
IF you want this to work both on iphone and ipad, and also want to get the title centered then use the following code.
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
label.text=self.navigationItem.title;
label.textColor=[UIColor whiteColor];
label.backgroundColor =[UIColor clearColor];
label.adjustsFontSizeToFitWidth=YES;
label.font = [AppHelper titleFont];
label.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView=label;
}
You can assign any UIView to a navcontroller's title area.
Create a UILabel and set its font and size anyway you want, then assign it to the UIViewController's navigationItem.titleView property. Make sure the UILabel's backgroundColor is set to clearColor.
This only works on the top-level nav-view. As the user drills down into the view controller hierarchy and the "back" button is shown the alternate titleView is ignored and the regular text label is shown.