Custom View with UILabel in it appears black - objective-c

I have created a custom UIView that has some set of UILabels in it. I add those UILabels inside the custom view inside its drawRect method.
But the whole custom view appears black on the screen. I have not set any background color for the custom UIView. How Do I fix this?
I tried setting background color to ClearColor but it still looks black.
I tried setting opaque property of the custom view to false and the view obviously disappeared.
Please help.

don't do that in drawRect: method which is intended to draw in the graphic context. If you want to add some specific subviews do it in an init / initWithFrame method.
For me the best way is to create a custom uiviewcontroller subclass and initialize it using a xib (nib) file. Working at controller level is a good practice.

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.text = #"Your label";
[newView addSubview:mytext];
[mytext release];
[self.view addSubview:newView];
[newView release];

Just incase someone stumbles upon this thread like I did in 2021. Check to see if you have accidentally toggled 'dark mode'. It will show similar visual 'issues' to the question above.

Related

Background colors in UITableViewController with UISearchbar

please look at the following screenshot.
It shows a UITableviewController with a UISearchdisplayController in the view header. I have set the background color of the table view to a somehow white color (see the below area). However the area above the UISearchbar does not have the same color (gray). How can I set / change this color? I tried different approaches like:
Adding additional subview
Seachbar background color
...
But till now i have had no success. Can anybody help me? Thanks in advance.
So i found the issue:
I was using:
self.tableView.tableViewHeader = self.searchDisplayController.searchbar;
Now I am using an aditional view with the searchbar as subview:
UIView *v = [[UIView alloc] initWithFrame:self.searchDisplayController.searchbar.frame];
[v addSubview:self.searchDisplayController.searchbar];
self.tableView.tableHeaderView = v;
Everything works fine now, the gray color is gone and the backgorundColors are equal.
you can play with
[[UISearchBar appearance] setBarTintColor:[UIColor redColor]];
or
[[UISearchBar appearance] setTintColor:[UIColor greenColor]];

How do you customise UISearchBar colors in iOS7?

I have searched quite a bit but cannot find a good answer to this.
I want to change the backgroundColor of the inner rounded view.
Like in Tweetbot on the search tap where it changes from gray to blue.
I understand that I probably need to iterate over the subviews but I don't know how to get the right object. (for the backgroundColor it's not the _searchLabel)
The default contrast of this element is so bad it's not even funny :(
Ok, this works. But note that you can't set a UIBarStyle beforehand or it will override everything.
https://stackoverflow.com/a/19836215/1252720
If you're still looking for a better answer, I just stumbled across this thread and found a great solution: UISearchBar text color change in iOS 7
If you look at the answer given by Sandeep-Systematix (not the accepted answer, but the answer right below), he mentions a really clean way to modify subviews in any class with this method:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
You can read more about this in Apple's documentation: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
That said, here's what you'll need to change the white, rounded background of the UITextField inside the UISearchBar:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor redColor]];
Now if you needed to create different UISearchBars with different styles, you would simply create a subclass of UISearchBar and you'd end up with something like this:
[[UITextField appearanceWhenContainedIn:[MyCustomSearchBar class], nil] setBackgroundColor:[UIColor redColor]];
Use this code.
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.contentView.bounds.size.width, 44.0f)];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
[self.view addSubView:_searchBar];
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];

How to display a UIWindow based dimmed background like UIAlertView?

Here's my problem: I tried to display a UIWindow with a subview that displays a radial gradient. I wanted to put the window on the UIWindowLevelAlert. I had this all figured out before and it was working, but now I'm trying to reproduce it wasting hours...
This is the code I've got (the background is not a gradient because I wanted to keep it simple, then add a view that really is like the one of an UIAlertView):
- (IBAction)buttonPressed {
UIWindow *backgroundWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
backgroundWindow.windowLevel = UIWindowLevelAlert;
UIView *backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.alpha = 0.5;
[backgroundWindow makeKeyAndVisible];
[backgroundWindow addSubview:backgroundView];
backgroundWindow.hidden = NO;
}
When a UIWindow is dealloc'd, it will be removed from the screen. Since you don't keep a reference to your UIWindow, I believe it is getting released and dealloc'd, and therefore it won't show.
The solution is to keep a reference to the window somewhere. Can you store it as a property in the class you're working in?

UIView background image not rotating

I've got an app that has two different background images. The one selected is determined by the orientation. When I start out, I check self.interfaceOrientation, and then go and pick the proper image. However, whenever the view opens, part of the image repeats instead of stretching. I saw a previous answer applying autoresizing masks to an imageview, but there is no imageview that I'm currently using.
Inside the loadView method:
if(self.interfaceOrientation==UIInterfaceOrientationPortrait ||self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: #"portrait"]]];
}else{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: #"landscape"]]];
}
As rishi had pointed out, the issue was caused by the colorWithPatternImage method. What I did to resolve this was to set the background of the view to be a specified image.
UIImageView* bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed: #"foo.png"]];
[self.view addSubview: bgView];
I also added in flexible width and height so that it would rotate properly.

UINavigationBar with customized background - how to make rightBarButton visible

I've customized background of navigation bar in my RootViewController (just part of code)
[navBar insertSubview:customBack atIndex:0];
I push detailViewController and add activity indicator as a rightBarButtonItem
UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
self.activityIndicator = actInd;
[actInd release];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:self.activityIndicator];
[self.navigationItem setRightBarButtonItem:barButton];
[barButton release];
The problem is that the indicator is not visible but without customized background it works OK.
You can better change your navigationBar Background I think. Create a subclass of UINavigationBar and add:
- (void)drawRect:(CGRect)rect {
UIImage * image = [UIImage imageNamed:#"MyNavigationBarBackground.png"];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1.0, -1.0); // Otherwise the image is drawn upside-down
CGContextDrawTiledImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
}
I do that and I never add a problem with any button :)
Zoleas has the right idea. If you don't or can't use a subclass, I think your problem is that you are adding your subview at index 0 so it is on top of the other views in the navigation bar, it is probably hiding your buttons.
set rightBarButtonItem with following way
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Exit" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
Try this and also check the position of background
The activity indicator is there its just not being activated. Try animating it like this:
[actInd startAnimating];
Or if you want to keep it visible set the hideWhenStopped property like this:
actInd.hidesWhenStopped = NO;
Did you try to change the origin from the UIActivityIndicatorView?
Or creating it inside an UIView and add this UIView to the UIBarButtonItem.
I think the problem can be the frame from the element.
By this way you can resolve the problem:
Create activity indicator and add in a view1
Now this view1 add or create navigation bar with the help of this view1. Now when view1 is visible automatically you will be able to see the loading indicator.
Just wild try on this.