iOS: Make layer opaque without fading text - iphone

I have a frame on my UIView with a layer. I have the layer set the background color and make the alpha 0.5 and make the frame.backgroundColor = clearColor so that people can see the lines behind it. However it makes the subviews which hold text also faded out. How do I prevent this?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setAlpha:kAlpha];
CALayer *layer = [self layer];
[layer setMasksToBounds:YES];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setCornerRadius:kCornerRadius];
}
return self;
}
- (id)init
{
if (self = [super init]) {
self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = NO;
tileTitle = [[UILabel alloc] init];
tileTitle.textColor = [UIColor blackColor];
tileTitle.backgroundColor = [UIColor clearColor];
tileTitle.font = [UIFont boldSystemFontOfSize:13.0f];
tileDescription = [[UILabel alloc] init];
tileDescription.textColor = [UIColor blackColor];
tileDescription.backgroundColor = [UIColor clearColor];
tileDescription.font = [UIFont systemFontOfSize:11.0f];
tileDescription.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:tileTitle];
[self addSubview:tileDescription];
}
return self;
}

To change the transparency of the view, but not its subviews, you can change its background color:
myView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.25];
This method also works with a CALayer.

you must not set alpha value on all uiview, you must se the color with alfa.
[self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];

Use a subview (with the same size as your UIView) and set the color and alpha on the subview to your desired tranparency, rather than setting the alpha of your view. Set the backgroundColor of the top-level UIView to clearColor.

Related

Label not showing up in overlay UIView

Ok, I have done this kind of thing a million times but this particular case escapes me and I'm sure its just something stupid I am missing...
But I am trying to display a UIView on top of a mapView to give the impression the mapview is 'disabled.'
I created a UIView subclass and added a single label. like so...
.h
#import <UIKit/UIKit.h>
#interface DisabledOverlayView : UIView
#property (nonatomic, strong) UILabel *disabledOverlayViewText;
#end
nothing complicated...
in my .m I set the text and add it to my UIView.
#synthesize disabledOverlayViewText = _disabledOverlayViewText;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
_disabledOverlayViewText.textColor = [UIColor whiteColor];
_disabledOverlayViewText.frame = CGRectMake(0, 0, 200, 200);
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self addSubview:_disabledOverlayViewText];
}
return self;
}
then the class i wish to use this overlay i call it with...
DisabledOverlayView *disabledView = [[DisabledOverlayView alloc] initWithFrame:CGRectMake(12, 12, _mapView.frame.size.width, _mapView.frame.size.height)];
disabledView.disabledOverlayViewText.text = #"No Location";
[self.view addSubview:disabledView];
None of this is too complicated.. but the label doesn't show up. the overlay appears just fine, but no label text. Ive messed with the frames of the label thinking maybe it was off the visible view but no dice. and i cannot figure out what is going on.
Replace your initWithFrame method with the below code. I have added the alloc init line and changes the background color of the label. Try it and see whether it works :)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
_disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_disabledOverlayViewText.textColor = [UIColor whiteColor];
_disabledOverlayViewText.backgroundColor = [UIColor blackColor];
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self.view addSubview:_disabledOverlayViewText];
}
It seems the label is not allocated and it's nil.
Try this
(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
self.disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_disabledOverlayViewText.textColor = [UIColor whiteColor];
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self addSubview:_disabledOverlayViewText];
}

Display vertical pages for UITextView in UIScrollView

How i can display vertical pages for UITextView in UIScrollview
self.textView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 410)]autorelease];
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont fontWithName:#"Georgia-Bold" size:14];
self.textView.textAlignment = UITextAlignmentCenter;
//self.textView.backgroundColor = [UIColor colorWithHue:2.0/12 saturation:2.0 brightness:4.0/10 alpha:1.0];
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.editable = NO;
self.textView.delegate = self;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
self.textView.layer.cornerRadius = 1;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"Juice fasting makes every other natural method work faster...............";
[self.view addSubview:self.textView];
scrollView.contentSize = CGSizeMake(textView.frame.size.width, textView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.delegate = self;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
[self.scrollView addSubview:self.textView];
[button release];
[super viewDidLoad];
}
I have a long text in UITextView. How i can display this UITextView in vertical pages when user scrolls.
Appreciate help.
Did you try paging?
textView.pagingEnabled = YES;
Is that what you want? Otherwise I probably would recommend using a default UILabel in a default UIScrollView, to set this up. (Adding one UILabel per page to the scrollView)
Make the text view large enough so that there is no scrolling within it (or change it to a UILabel), and put that inside a separate scrollView.
Then set
scrollView.pagingEnabled = YES;
and make sure the height of the content size is larger than the height of the actual scroll view. So if the text view is 320x960, make the scroll view 320x480.

different custom color for uinavigationbar and items

In my app I am overriding the uinavigationbar color in the AppDelegate to create this color across the entire app:
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor colorWithRed:0.16
green:0.20
blue:0.32
alpha:1];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
[self setBarStyle:UIBarStyleBlack];
[self setTintColor:color];
}
However, in one of my views, i would like to change the color of one of the nav bar items to another color, different from the global color above, but only for one of the items - the bar color should stay the same (reasoning - i'd like to have a nav bar item in green and a "On" text and change it to red with an "Off" text based on the user input).
I tried to override the color of the button in my view in the following way, but it doesnt seem to do anything.
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTintColor:[UIColor greenColor]];
}
Does anyone have a suggestion (or a better way) to make this happen?
Cheers.
It is tested code which will work 100%.
//in below code u can set the different image for different color
or
simply
fill the color by your question code.
here below is for put image in navigation bar
u can customize by removeing the picture code and puttting ur above code with different color.that it .logic is same
CustomNavigation.h
#import <Foundation/Foundation.h>
#interface UINavigationBar (UINavigationBarCustomDraw){
}
#end
CustomNavigation.m
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.5f
green: 0.5f
blue:0
alpha:1]];
if ([self.topItem.title length] > 0) {
if ([self.topItem.title isEqualToString:#"First"]) {
[[UIImage imageNamed:#"First.png"] drawInRect:rect];
}
else if ([self.topItem.title isEqualToString:#"Second"]) {
[[UIImage imageNamed:#"Second.png"] drawInRect:rect];
}
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
[label setBackgroundColor:[UIColor clearColor]];
label.font = [UIFont boldSystemFontOfSize: 20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.topItem.title;
self.topItem.titleView = label;
}
else {
[[UIImage imageNamed:#"wood.png"] drawInRect:rect];
self.topItem.titleView = [[[UIView alloc] init] autorelease];
}
}
#end
if u want to First.png to set navigationBar background image in FirstViewController then
in ur FirstViewController.m
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=#"First";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}
if u want to Second.png to set navigationBar background image in SecondViewController then
in ur SecondViewController.m
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.title=#"Second";
[self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)];
}

changing the background color of custom cell

It looks simple thing but i dont know why i am not able to change the background color of a custom tag on which i am working. Please check my code below for the custom cell.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textColor = [UIColor orangeColor];
self.textLabel.text = #"lklklk";
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
self.contentView.backgroundColor = [UIColor blackColor];
self.accessoryView.backgroundColor = [UIColor blackColor];
}
return self;
}
The cell is only displaying the above text with white backgroud
Thanks in advance
I've had to deal with this challenge myself while working on Spring Cleaning
Here's how I solved the issue:
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor blackColor];
view.opaque = YES;
self.backgroundView = view;
[view release];
UITableViewCells have a backgroundView which you need to hide in order to set the backgroundColor:
[self.backgroundView setHidden:YES];
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;

viewing tableViewLines on background of custom UIView

I have a viewController that has a tableView and a custom loading UIView that displays a loading message and spinner while the data of the tableView is being loaded.
The custom UIView has a red background that i can see, but i can still see the lines of the tableView, How can i get to display the custom uiview without seeing the tableView lines on the background.
#implementation LoadingView
#synthesize spinner;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
[self setUserInteractionEnabled:NO];
[self setBackgroundColor:[UIColor blueColor]];
// Spinner
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(320/2, 150)];
[self addSubview:spinner];
[spinner startAnimating];
//title label
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Loading...";
titleLabel.font = [UIFont boldSystemFontOfSize:18];
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor blackColor];
titleLabel.textAlignment = UITextAlignmentLeft;
CGFloat height = [titleLabel.text sizeWithFont: titleLabel.font constrainedToSize: CGSizeMake(300, 1500) lineBreakMode: UILineBreakModeWordWrap].height;
[titleLabel setFrame: CGRectMake(120, 180, 100, height)];
[self addSubview:titleLabel];
[titleLabel release];
}
return self;
}
- (void)drawRect:(CGRect)rect {
}
- (void)dealloc {
[super dealloc];
[spinner release];
}
#end
You can set the lines "off", when you start you loading process, setting them transparent.
// Setting transparent
tableView.separatorColor = [UIColor clearColor];
And when you remove the loading UIView, you change the color for the one that you want.
// Setting the desired color
tableView.separatorColor = [UIColor grayColor];
Another alternative is to set the table view hidden while you don't have data to be displayed, and than when the first or more row appear you place you set the table view to not hidden.
I hope this will help you!
Cheers,
VFN
The problem was that self.view in the viewController had the tableView assigned directly. The solution is to assign the self.view a UIView and add the tableView and the custom UIView on top of self.view.
This is sample code from the viewController.
- (void)loadView {
UIView *aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//tableView
self.tableView = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease];
//set the rowHeight once for performance reasons.
//self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = 75;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.autoresizesSubviews = YES;
[aView addSubview:tableView];
//set the rowHeight once for performance reasons.
self.view = aView;
[aView release];
}
- (void)viewDidLoad {
[super viewDidLoad];
loadingView = [[LoadingView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:loadingView];
//fetch productsArray
[self productListRequestStart];
}