Make a custom title view for a backBarButtonItem - objective-c

I am making a custom backBarButtonItem with a PNG and that works just fine. The only question is how to change the text color. The way I did that on my navigation bar was this:
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"aColor.png"]];
self.navigationItem.titleView = label;
label.text = #"aTitle";

dont use autorelease for set label to titleview,after assign them release them
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"aColor.png"]];
label.text = #"aTitle";
self.navigationItem.titleView = label;
[label release];

Related

objective-c ios auto layout NSLayoutConstraint

UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, frameWidth, frameHeight)];
headerView.backgroundColor = UIColorFromRGB(0x5F70B9);
UIImage* leftImage = [UIImage imageNamed: #"search_list"];
UIImageView* leftImageView = [[UIImageView alloc] initWithImage: leftImage];
[leftImageView setFrame: CGRectMake(10, 15, 70, 50)]; // CGRectMake(10, 15, 70, 50)
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(80, 20, 160, 40)];
textView.backgroundColor = UIColorFromRGB(0x5F70B9);
textView.textColor = UIColorFromRGB(0xFFFFFF);
[textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3]];
textView.text = STLocalizedString(#"message_overall_non_reply");
textView.editable = NO;
UIImage* rightImage = [UIImage imageNamed: #"arrow_mask"];
UIImageView* rightImageView = [[UIImageView alloc] initWithImage: rightImage];
[rightImageView setFrame:CGRectMake(380, 30, 15, 20)];
[headerView addSubview: leftImageView];
[headerView addSubview: textView];
[headerView addSubview: rightImageView];
Expect image:
enter image description here
Now i am hardcoding the origin x, y and everything. I want to use auto layout so that the left image and right image act as leading and trailing icon.
any suggestion?
Tried something like below, but not working:
`
[leftImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *leftImageViewConstraint = [NSLayoutConstraint constraintWithItem: leftImage attribute: NSLayoutAttributeLeading relatedBy: NSLayoutRelationEqual toItem: headerView attribute: NSLayoutAttributeLeading multiplier: 1 constant: 0];
[leftImageView addConstraints: #[leftImageViewConstraint]];
`
You need to activate each constraint that you add. When you have a bunch of constraints to set it's a lot easier to use NSLayoutConstraint activateConstraints. And using the various "anchor" properties for setting up the constraints is simpler than using the long form of creating an NSLayoutConstraint.
Here's your code updated with lots of constraints:
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, frameWidth, frameHeight)];
headerView.backgroundColor = UIColorFromRGB(0x5F70B9);
UIImage* leftImage = [UIImage imageNamed: #"search_list"];
UIImageView* leftImageView = [[UIImageView alloc] initWithImage: leftImage];
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.backgroundColor = UIColorFromRGB(0x5F70B9);
textView.textColor = UIColorFromRGB(0xFFFFFF);
textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle3];
textView.text = STLocalizedString(#"message_overall_non_reply");
textView.editable = NO;
UIImage* rightImage = [UIImage imageNamed: #"arrow_mask"];
UIImageView* rightImageView = [[UIImageView alloc] initWithImage: rightImage];
leftImageView.translatesAutoresizingMaskIntoConstraints = NO;
textView.translatesAutoresizingMaskIntoConstraints = NO;
rightImageView.translatesAutoresizingMaskIntoConstraints = NO;
[headerView addSubview: leftImageView];
[headerView addSubview: textView];
[headerView addSubview: rightImageView];
[NSLayoutConstraint activateConstraints:#[
[leftImageView.leadingAnchor constraintEqualToAnchor:headerView.leadingAnchor constant:10],
//[leftImageView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:15],
[leftImageView.centerYAnchor constraintEqualToAnchor:headerView.centerYAnchor],
[leftImageView.widthAnchor constraintEqualToConstant:70],
[leftImageView.heightAnchor constraintEqualToConstant:50],
[rightImageView.trailingAnchor constraintEqualToAnchor:headerView.trailingAnchor constant:-10],
//[rightImageView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:30],
[rightImageView.centerYAnchor constraintEqualToAnchor:headerView.centerYAnchor],
[rightImageView.widthAnchor constraintEqualToConstant:15],
[rightImageView.heightAnchor constraintEqualToConstant:20],
[textView.leadingAnchor constraintEqualToAnchor:leftImageView.trailingAnchor constant:10],
[textView.trailingAnchor constraintEqualToAnchor:rightImageView.leadingAnchor constant:-10],
[textView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:20],
[textView.bottomAnchor constraintEqualToAnchor:headerView.bottomAnchor constant:-20],
]];
I made a few guesses here. Obviously you can change these to suit your needs.

contentOverlayView on TVOS not showing

myview = [[AVPlayerViewController alloc]init];
[window.rootViewController.view addSubview:controller.view];
UILabel *label = [[UILabel alloc] init];
label.text = #"TEST";
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Menlo" size:(72.0)];
[myview.contentOverlayView addSubview:label];
myview.view.frame = window.rootViewController.view.frame;
// Disable playback controls
myview.showsPlaybackControls=false;
// show the view controller
[window.rootViewController addChildViewController:myview];
myview is playing video, but the "TEST" label isn't visible. I'm not sure what I'm doing wrong.

navigationItem titleview align center between buttons

I have the code likes, in the title bar I am having 3 buttons, left search button, right camera button and option button, in between I am having title.
What I want to do is, I want to display the title centered between the search button and Camera button, can anyone help me how to do this.
//Left Button
UIBarButtonItem *leftSearch = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btn_search"] style:UIBarButtonItemStyleBordered target:self action:#selector(gotoSearch)];
leftSearch.imageInsets = UIEdgeInsetsMake(0, -5.0f, 0, 0);
[leftSearch setTintColor:[UIColor whiteColor]];
/////////
//Right Button -- First Camera
UIBarButtonItem *rightCamera = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"icon_camera"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(gotoPhotoGallery)];
[rightCamera setTintColor:[UIColor whiteColor]];
rightCamera.imageInsets = UIEdgeInsetsMake(0, 0, 0, -59.0f);
///////////////////
//Right second Button -- for options menu
UIBarButtonItem *rightMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"header_option"] style:UIBarButtonItemStyleBordered target:self action:#selector(gotoPhotoGallery)];
[rightMenu setTintColor:[UIColor whiteColor]];
rightMenu.imageInsets = UIEdgeInsetsMake(0, 0, 0, 5.0f);
///////////////////
NSArray *buttons = #[rightMenu, rightCamera];
self.navigationItem.rightBarButtonItems = buttons;
[[self navigationItem] setLeftBarButtonItem:leftSearch];
//Title Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Museo Sans" size:20];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = LoginUserName;
self.navigationItem.titleView = label;
[label sizeToFit];
Create your title label with the same width and height as the navigation bar. For example
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,44)];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = LoginUserName;
titleLabel.textColor = [UIColor whiteColor];
[self.navigationController.navigationBar addSubview:titleLabel];
This will put your text in the middle.

self.navigationItem.titleView cutting off UIBarButtonItem after rotation

I use a self.navigationItem.titleView and I set two UIButtons as the self.navigationItem.rightBarButtonItems in the UINavigationBar. I have achieved this in my navigationBar like so
Whenever I rotate the view controller landscape and then back, the uibarbutton item gets cut off like so
The code I use to set the titleView is this:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.numberOfLines = 2;
titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = self.tabTitle;
self.navigationItem.titleView = titleLabel;
I have no idea why this is happening and I wonder if if any of you know how to fix this.

setting transparent background for UILabel for iphone application

I have a UILabel in UITableView. Here is the code i have written in cellForRowAtIndexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(30.0, 5.0, 250.0, 35.0) reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = CGRectMake(30.0, 5.0, 250.0, 35.0);
UILabel *label = [[UILabel alloc] initWithFrame:rect];
label.opaque = NO;
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 2;
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor darkGrayColor];
label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[label setText:#"Test Message"];
[cell addSubview:label];
[label release];
cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
My intention was to set the background of cell and UILabel as transparent.
But it is not working. can any one tell me what i am missing here
Thanks
Sandeep
Change the following line:
label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
to this:
label.backgroundColor = [UIColor clearColor];
For those using Xamarin.iOS it's obviously:
UILabel myLabel = new UILabel();
myLabel.Text = "Hello world!";
myLabel.BackgroundColor = UIColor.Clear;
[label setBackGroundColor:[UIColor clearColor]];
To set the clearBackGround Color for a label