UIView drawRect sub rectangle - ios

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.

Related

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.

CGContextClearRect ignores alpha value set in storyboard

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.

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?

Can I draw the background of a UINavigationBar as the background of my custom view?

I have a custom view which I would like to look like a UINavigationBar. Is there a way for me to draw the background the same way a UINavigationBar would?
I don't want to draw an image or a gradient fill that looks like a UINavigationBar - I want to use the same library code (if it is public) that a UINavigationBar does to draw its background.
The drawRect: method of a UINavigationBar is private - you can't use it to draw the background of your own view.
The easiest way round this would be to take a screen shot, trim the image to nav bar size, add to your project, and then either draw the image in your view's drawRect: method, or add a UIImageView as a subview of your view.
Alternatively, would it be possible to use a UIToolbar instead of your custom view?
You could set your view background image and just use an image that has the navigation bar look already filled in.
As far as tapping into whatever the navBar uses to create itself, you are probably out of luck unless you just build a navBar into your app. Otherwise you would end up having to build a custom view at the top of your app, draw everything in manually. At that point, you are probably better off implementing the navBar.

Resources