CGContextClearRect ignores alpha value set in storyboard - ios

I'm using CGContextClearRect method in MyView's drawRect to clear MyView to background color. However, I must explicitly set the background color alpha value of MyView in code. If I set the alpha value in storyboard, the CGContextClearRect makes MyView black. So why the alpha value set in storyboard doesn't do the trick?

The drawRect method allows you to reset the view's layer content when flagged for a refresh. There's is no need to call CGContextClearRect as clearing the layer is automatically managed by the system before it calls the drawRect method, effectively providing you with a blank canvas for the view's layer.

Related

Why is my viewController background color permanently white?

If I change the background color and run the app the color works fine, but as soon as I add subViews like a tableView or a containerView the still visible parts of the background becomes white.
I have tried:
- To set the color programatically. Still white.
- To add another UIView on the superView, add the subViews to that and changed the color on the UIView instead. Still white.
- While on the same approach as above, changed the superView color and set the UIView color to clear. Still white.
- Changed the alpha value to 0, but this makes the subViews invisible as well.
This is the hierarchy:
Have never run into this problem before and I don't understand it...
Check every outlet in storyborad if it has a background color of white . Or self.view.backgroundcolor to clearcolor may work , There must be a view under the view which color is white. Or sometimes there needs time to load the storyboard and this causes some undefined behaviour.You can try a dispatch_time_after in viewdidLoad and then start your code from there.

UIView drawRect sub rectangle

I have a UIView whose backgroundColor is UIColor.clear. I need to perform drawRect in a subrectangle within the bounds of this view but also want to set the background color of that subrect to be black color with 0.5 transparency. I need to always invoke drawRect on that subrect whenever I want. How do I do that?
According to the documentation, you don't invoke drawRect: directly, but to redraw the view (i.e. set or unset your black background colour), you call setNeedsDisplay on the view which will invoke drawRect on the next draw cycle.
This will allow you to set your black background and draw whatever else you want in there, but I'm pretty sure a view can't have different alpha values for different subregions. For the alpha, this might be what you're looking for.

Changing the alpha of UIView affects the font opacity

Quite simple question: I want to change the opacity of UIView and keep font above it with alpha = 1.0.
So as you can see there are two labeles and under them is UIView. I've set the opcaity of the UIView (for example: #IBOutlet weak var coView: UIView! self.coView.alpha = 0.8). The result is that both UIView and labels are more transparent and I want to keep label's text on alpha = 1.0. Is it possible to do without changing stack views and constraints?
Use
coView.backgroundColor = UIColor.black.withAlphaComponent(0.8)
It's super annoying and not precisely what you wanted, but it's a way around it. It sets the background color of the coView with an alpha value, which effectively gives you what you want.
Instead of changing the Alpha of the Object you should leave the alpha at 1 and choose a custom Background colour (in the Attributes inspector):
Then in the custom colour widget change the opacity of the colour as desired.
Changing the alpha of the UIView changes how all its subviews are seen to the new alpha set. There no way to actually avoid this, but you can go around this by using some other auxiliary views.
For example, inside your UIView pm, you could have another subView called backgroundView, which is on the back and that's the one you set the background color. Every other element inside your UIView pm would be on top of the backgroundView with a clear background color.
If you want to only change the alpha of the color, you just change the alpha to the backgroundView and the other elements in UIView pm will remain with the same alpha

Difference between setting self.view.opaque = NO and setting the background color to clearColor?

Just wondering what would the difference be between setting the opaque property of a (background) view to NO, vs. simply setting the background colour of a view to 'clearColor'. If I do the latter, would it be the case then that there would be no need to also set the opaque property to NO (as in self.view.opaque = 0)?
All the difference in the world. They have nothing whatever to do with each other. Neither has any effect on the other.
opaque, if YES, sends a message to the drawing system about whether or not it can save some cycles by not having to composite this view with what is behind it.
backgroundColor is, uh, the color of the background. If the background color is opaque and the view's alpha is 1, it is your duty to set opaque to YES. It will not happen all by itself.
Big hint: I do hope you are not confusing opaque with layer's opacity or view's alpha. That is yet another thing.
The UIView.opaque documentation provides a lot of detail around this and discussion as to how it works.
The opaque property is not the same as setting backgroundColor to clear.
Nor is it the same as setting the alpha value of the view.
The only thing it affects is how the OS draws the view inside the drawRect method. Allowing it to skip rendering passes for the view of set to YES.
It's all here in the docs.
N.B. Always read the documentation.

resetting the background image of a UITextField doesn't work

Setting a custom background image (for iOS < 5) in the style of a tinted rounded rect text field works fine,
yet I can't change it back to normal or not even to another background image, once it's set.
I'm calling setBackground:nil and setBorderStyle:UITextBorderStyleRoundedRect on the view in order to reset it.
There is some effect: The edge of the border gets slightly lighter, as if the desired view is drawn behind it, but it's not set back to the normal appearance. Calling setNeedsDisplay doesn't help either.
Am I missing another property?

Resources