UIView background color with alpha in xib - objective-c

I have a problem. I am trying to make my custom UIView with an image in the very center. The part outside of that image got to be transparent and grayed.
In xib for "View" I've set this:
The result - fail. The region outside the image is just gray, not transparent.
But if I add
-(void) viewDidLoad
{
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
}
Then everything works as expected.
What was I doing wrong when trying to achieve transparent backround in xib?
UPDATE:
I found the code which was doing presenting of the view, and there was alpha set inside of it. So I guess that was the problem. Thanks. Sorry for being so newbie

Maybe this one is what you expected. You can only change the background Color rather the view's alpha. Just set opacity with the background Color.

When setting the alpha of a parent UIView, the alpha value will be passed to the child views, giving the same alpha to all the child views. If you set the alpha value of the UIView to 1, you are setting the alpha of the view, not the color.
That said, you can set the alpha of the background color. When you do this, the view maintains its own alpha value, while the background color has its own alpha value. Hope this helps

The issue was in custom show method. It didn't use presentViewController:animated:completion: method. It used alpha animaiton from 0 to 1. The second - when using standard presentViewController:animated:completion: method cocoa optimization was hiding the underlying view, so I need to set up (for iOS 8)
vc_to_present_on.modalPresentationStyle = UIModalPresentationCurrentContext;
vc_to_be_presented.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
And then everything work as expected

Related

How to make a view transparent without transparent subview on that

I have a probem like this. We know we can make transparent an UIView by changing Alpha value of that view. But I dont want to make transparent buttons which are inside that view. My problem is when I set the parent view alpha, button also get transparent and button title not visible clearly. How can I overcome this problem.
Set the background color of the UIView with variable alpha, like this:
view.backgroundColor = [[UIColor white] colorWithAlphaComponent:0.5f];

How do I stop view controllers from being clear color?

I have created a screen tutorial for my app.
I've done this by creating a PageViewController to manage 4 viewControllers.
In the PageViewController I have implemented the following code
self.modalPresentationStyle = UIModalPresentationFormSheet;
I have also set the alpha on the pageViewController view to .45
This makes the PageViewController transparent which is exactly what I want.
However, it is also making everything inside the 4 viewControllers that are being managed by the PageViewController transparent i.e. buttons, labels, etc.
How can I stop all of those object from being transparent?
Views always work like this. If you wanted to make a view semi-transparent, it would usually be pretty vexing if that didn't also affect all of its subviews. The times when you want your alpha setting to also affect the subviews likely far outnumber the times when you don't.
What you can do instead of making the view transparent is to make its background color transparent. That is, instead of:
self.view.backgroundColor = [UIColor blueColor];
self.view.alpha = 0.45;
you can do:
self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:.45];
That way, your subviews are not affected, because while the alpha of your main view's background color is 0.45, the alpha of the view itself is still 1.0.
To change the background color of a view, use the following on the view:
[viewController.view setBackgroundColor:[UIColor COLOR]];
where COLOR is the color you would like (i.e. whiteColor)

UILabel gets hidden behind UIImageView

I have made a subclass for UITableViewCell and I am implementing Subtitle TableViewCell with a thumbnail image.
Following are the UITableViewCell contents:
The issue I am facing is when the data loads in TableViewCell, the subtitleLabel text gets hidden upto the height of the imageView. But when I select any Cell, it shows subtitleLabelText completely.
I have added the screenshot of the same for complete reference:
The UIImageView has frame = CGRectMake(0,0,40,40);
Try to give a clearColor background color for the cell title label -
cell.textLabel.backgroundColor = [UIColor clearColor];
It turns out I was using TableViewCell style as subtitle instead of custom. The style settings in subtitle was making the other labels to hide below them. What a silly miss!
In your nib or storyboard file, make sure that the label is below the image view in the list of subview components (it is in the left of the screen). The first subview in that list will be at the lowest level (behind every other subview, if they overlap).
write one line of code
Titlelabel.backgroundcolor = [UIColor ClearColor];
because your label has white background..and Titlelabel height is too large so label is colliding.
Let me know working or not!!!
Happy Coding!!!
What is the frame of Title Label? if its height is more, then also it may possible that it hides your subtitle Label
Here's a great tutorial which helped me when I was trying to do something like you want :
http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/
You can adapt the size of the different components (ImageView, TitleLabel, Subtitle,...)

Draw Over Image

I'm working on some drawing code. I have that portion working great.
I want to draw over an image, but I want to still be able to see the detail of the image, the black lines, etc.
What I am working on is making a transparent UIImageView that holds the image.
I'm not sure how to get this set up properly though.
Should this be added above the other UIImageView that I color on or below it?
Here's what I have so far:
- (void)viewDidLoad
{
[super viewDidLoad];
topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 46, 320, 370)];
[topImageView setImage:[UIImage imageNamed:#"imagesmall.png"]];
topImageView.alpha = 1.0;
topImageView.layer.opacity = 1.0;
topImageView.layer.opaque = NO;
[self.view addSubview:topImageView];
[topImageView release];
}
Thoughts anyone?
Yes, you can draw views over other views. They are drawn in the order that they're added as subviews, unless you reorder them after that.
You may need to set the opaque property for some views (this is distinct from and overrides their layer opacity), and set their backgroundColor to nil. UIImageView seems to be transparent by default, as long as its image is; some other UIView subclasses are not.
So, just what is your overlay going to be? If you just need to display one image over another, what you have here seems to work already. If you need to draw some lines programmatically, you'll need to do this:
Create a subclass of UIView.
Implement its drawRect method to display the content you need.
When you add your custom view on top of the background image, make sure it is not opaque and has no backgroundColor.
A common problem here is to find that your foreground is working, but the background isn't being loaded properly. To make sure the background is there, set the alpha of the foreground view to 0.5. You won't want to do that in production, but it will allow you to verify that both views exist.

100% opacity UILabel over a 50% opacity background (UIView?)

So right now I have a UIView with a UILabel in it. I want the background to have an opacity < 1.0 and the label to have an opacity of 1.0. However since alphas propagate down the view hierarchy, the label ends up with an opacity < 1.0 as well.
Is there anyway to do what I want without making the UILabel a subview of another view??
Just set the background color to be semitransparent:
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
Or, in Swift:
view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
Or, Swift 3:
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
Note that, in this particular case, UIColor(white: 0, alpha: 0.5) is more concise, but colorWithAlphaComponent will work in general.
Besides being available in code, you can do this quite easily from iB as well:
Within the storyboard, select the view you wish to edit;
From the right panel, make sure the Attributes inspector is opened;
Click on the right side of the "Background" drop down box and choose "Other ..."; it will open a colour picker dialog;
Change the "Opacity" at the bottom to set the background colour opacity.
You can set the background color of the UIView with a semi-transparent color or make the image itself semi-transparent. This way it's a property of the view that is transparent, not the view itself.
You can use this:
self.view.layer.opacity=0.5