SetCenter problem for an UILabel - iphone

OK, I'm struggling to make this work but without success.
Basically I want to add a UILabel to an UIView and center it.
The code looks like this:
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[customTitleView setBackgroundColor:[UIColor clearColor]];
// Screen title
CGSize constraint = CGSizeMake(200.0, 44.0f);
CGSize size = [screenTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width, size.height)];
[titleLabel setText:screenTitle];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
titleLabel.center = customTitleView.center;
[customTitleView addSubview:titleLabel];
[titleLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
I expected that the UILabel would be centered within the UIView.
Well, it isn't. It is somehow right aligned and not even close to the center of the UIView.
What am I doing wrong?

So here's what's going wrong:
self.navigationItem.titleView = customTitleView;
When you set customTitleView as the title view of the navigation item the frame of customTitleView will change. customTitleView won't be 320 pixels wide anymore, but the frame of titleLabel will remain the same. Thus it will no longer be centered.
Can't you just set titleLabel as titleView? I think it should be centered automatically. If not, I know a more complicated way to solve it. Drop a comment and I'll tell you how.
EDIT: So here is how you realign the titleLabel after customTitleView's frame has changed.
When the frame of a view is changed layoutSubviews is called on that view. So instead of letting customTitleView be a regular UIView you need to make it a custom view that inherits from UIView. In that custom view you override layoutSubviews and in your implementation of layoutSubviews you make sure everything is aligned the way you want based on the new frame (self.frame). I'll leave the implementation to you.

Related

right align self.navigationItem setTitleView

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!

UIScrollView Inside UITableViewCell

I'm trying to add a UIScrollView inside my TableViewCell. It appears to be going well, however when the label comes in from the right as I'm scrolling, it comes in above the cell, but as the text moves to the left edge of the label, it disappears behind the label like I want it to.
Any suggestions?
UIScrollView *previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, 80.0)];
previewScrollView.backgroundColor = [UIColor clearColor];
[previewScrollView setContentSize:CGSizeMake(500, 60.0)];
previewScrollView.showsHorizontalScrollIndicator = YES;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 3.0, 500.0, 20.0)];
headerLabel.font = [UIFont boldSystemFontOfSize:14.0];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.text = #"My long text string that I hope work out ok";
[previewScrollView addSubview:headerLabel];
[[cell contentView] addSubview:previewScrollView];
//[cell addSubview:previewScrollView];
[previewScrollView release];
from the right as I'm scrolling, it comes in above the cell
Above the cell as in y or z axis?
If it is above in the y axis, you could try setting clipsToBounds on the scroll view. That makes it so subviews are only drawn within the area of their superview.
If it is above in the z axis, where something in front should be behind, try using:
[[cell contentView] insertSubview:previewScrollView atIndex:0];
inserting at index 0 ensures a view is behind all other subviews in the superview.

UITableview background view colour -iphone

I have an UITableview controller without an XIB.
I set the background colour of the tableview by doing this:
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"MmyImage.png"]]];
My image is a gradient image, so eventhough the above code is ok, it redraws the image for each cell when the tableview goes to edit view, and it appears as lines which is not looking elegant.
I would like to set the tableviews background to clear colour and set the superview's colour to the image, so that the tableview transitions smoothly over the superview. However the below code does not work:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];
This makes the background completely white, I dont know why.
Help appreciated..thanks.
Edited:new code but this does not work as well:
UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.tableView.window.bounds;
[self.tableView.superview addSubview:imgBg];
//[self.view addSubview:imgBg];
//[self.tableView.window sendSubviewToBack:imgBg];
//[self.view bringSubviewToFront:self.tableView];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[imgBg release];
In a custom UITableViewController subclass, self.view will return the same as self.tableView. So what you're doing there is altering the same object. In the end, you get to see the UIWindow. So to get the desired result, you should do this –
[self.view.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];
UITableView has a backgroundView property. Use it.
aTableView.backgroundView = [[[UIView alloc] initWithFrame:aTableView.bounds] autorelease];
aTableView.backgroundView.backgroundColor = backgroundColor;
First in your xib file, add a parent view. add table view in that parent view. set parent view's background color to your desired background. then set your tableview's background color to [UIColor clearColor].
Check also, if your table view is a group table style or not? if it is group table style, then the cell have distinct background. Then you need to clear the background of each cell. The best way to do is,
create a blank UIView *bkview = [[UIView alloc] initWithRect:CGRect(0,0)];
set this
as your cells background view.
I hope this will work.
then your second code will be right, except:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"myImage.png"]]];
replace this:
UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.view.bounds;
[self.view addSubview:imgBg];
[self.view sendSubviewToBack:imgBg];
Edit
I think:
[self.tableView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]]; will helps, that is the only difference now.

how to change the size of UILabel

I have a UILabel coded programmatically. I want to change the size of the label when i pressed a button. how to change the size of that label? this is my code
UILabel *theLabel11 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,30)];
[theLabel11 setText:#"US"];
[theLabel11 setTextAlignment:UITextAlignmentCenter];
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
[theLabel11 setBackgroundColor:[UIColor orangeColor]];
[theLabel11 setTextColor:[UIColor blackColor]];
[scroll1 addSubview:theLabel11];
You should declare your label as class property, so it can be accessed from other methods
To change the font size use
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
To change the frame size of the label us
theLabel11.frame = CGRectMake(x, y, width, height);
A common idiom for adjusting the spatial information on a UIView is as below
label.frame = CGRectMake(
x,
y,
width,
height
);
You can get the old position and height via
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height
If there is only one label added to scroll1 then, iterate the scrollview to get the label reference as follows in button action
for(UIView *subView in scroll1.subViews){
if([subView isKindOfClass:[UILabel class]]){
UILabel *lbl=(UILabel*)subView;
//change size of label here
}
}
if there are many labels assign a tag to each label while creating and check that in for loop

UINavigationBar Bottom Separator Line Color

How can I change the line's color that separates the navigation bar and the view?
For instance flickr changed it to gray (http://www.geardiary.com/wp-content/uploads/2009/09/Screen-shot-2009-09-08-at-8.00.06-AM.png)
By default mine is always black...
Thanks in advance for your help,
nico
They used a custom bottom bar and not the Apple provided ones. I dont know your setup, but if you can make or draw your own custom view however you want (you can do this), and stick buttons on it (you can do this too), then you have a toolbar
#define TOOLBAR_HEIGHT 44
CGRect frame = CGRectMake(self.view.bounds.size.height - TOOLBAR_HEIGHT, 0.0, self.view.bounds.size.width, TOOLBAR_HEIGHT);
UIView *customBottomBar = [[UIView alloc] initWithFrame:frame];
[customBottomBar setBackgroundColor: [UIColor grayColor]];
UIButton *button = [[UIButton alloc] initWithFrame:<frame goes here>]
... <button setup>
[customBottomBar addSubview:button];
[button release];
...<more buttons>
...<more buttons>
[self.view addSubview:customBottomBar];
[customBottomBar release];
And to answer your question, you can add whatever you want to any view, so while the way I just suggest is the most customizable, you might just want to place a 1pixel high colored bar at the right spot (on top of the existing toolbar) by doing this:
CGRect frame = CGRectMake(self.view.bounds.size.height - TOOLBAR_HEIGHT, 0.0, self.view.bounds.size.width, 1);
UIView *customLine = [[UIView alloc] initWithFrame:frame];
[customLine setBackgroundColor: [UIColor grayColor]];
[self.view addSubview:customLine];
[customLine release];