UIView border cover sub-view? - ios

I have a UIView which includes a UIButton which is partially on UIView. I have a problem when I draw a border on my UIView. Please have a look at my screenshot:
You can see the border is above the UIButton, why? Can anybody suggest? Thanks

Thanks for aăâ, I found a solution.
Basically the border is always drawn on top of everything
What I did is:
Create a UIView with color of border
Create another UIView as the child the main UIView which is a little bit smaller than the first one. The color of this newly create UIView is the main color
Here is the code:
self.layer.cornerRadius = 15;
self.layer.masksToBounds = YES;
self.backView.layer.cornerRadius = 15;
self.backView.layer.masksToBounds = YES;
The result is:
It's more or less what I need although it's not perfect.

It could have to do with the order that the objects are drawn. In your storyboard's "Document Outline", views that are lower down in a view controller's outline are drawn later. Perhaps the button is not the last drawn view, like you want?

Related

how to make visible other components in transparent UIVIEW in swift 5

I have used a UIVIEW . I want to make transparent the UIVIEW. But when I want to chage alpha 0.0 from storyboard for make UIVIEW transparent, textfield, label and other component also transparent. I want to make transparent UIVIEW, not other component of the view. Here is the image
Please help me to make visible other components in UIVIEW transparent
Set your UIView's backgroundColor to UIColor.clear.
Instead of changing the alpha of the UIView, you can make the background color of your UIView as white with alpha component 0.5 or something
myView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
If you want it to be completely transparent, you can set backgroundColor to clear from storyboard as well as code.
myView.backgroundColor = .clear
You just need to change the transparency not of the UIView itself, but of its
background color.
For example:
I set the color of the UIView transparent, while the UILabel remained its settings.
In order to create a semi transparent color, you can use the following code:
yourView.backgroundColor = UIColor.white.withAlphaComponent(0.5)
Hope this will help you!
Go to Main.Storyboard and select the view you want to make transparent.
From the Attribute Inspector select background color as custom.
Then set your desired color and decrease the percentage of Opacity of the color.

Draw border "within" UIViewController

I need to draw a visible border that goes around the edge of the screen within my view controller. I tried setting the view's borderWidth and borderColor, but nothing appeared on the screen. How would I accomplish this?
I tried the following code and it worked.
self.view.layer.borderColor = UIColor.orangeColor().CGColor
self.view.layer.borderWidth = 3

Working with transparency: View behind transparent view should only shine through and not overlap

I'm currently working with transparency and I'd like to have the following effect:
As you can see the line takes the full width of the view. There are some transparent UIViews also on this screen. The line seems to be below the transparent UIView and if it overlaps it has another color, because of the transparent UIView above. When there is no overlapping the line get its normal color.
How can I get this effect?
I tried to set the background of the UIView to transparent, but it didn't helped. The line has its normal color and doesn't interact with the transparency. Furthermore I tried to change the transparency of the view itself but with the same wrong result.
The rectangle above is done with code
UIView rectangle = new UIView (new CGRect (10, 10, 200, 120));
rectangle.BackgroundColor = UIColor.FromRGBA (204,115,225, 50);
UIView line = new UIView (new CGRect (0, 105, 320, 1));
line.BackgroundColor = UIColor.Red;
View.AddSubview (line);
View.AddSubview (rectangle);
line.SendSubviewToBack (rectangle);
the rectangle below is created in iOS designer.
Am I missing something?
Its all about the z order of the Subviews.
line.SendSubviewToBack (rectangle);
is probably not what you want.
If you add the line with its normal red color and add a UIView that has its alpha dropped to e.g. 0.5, then you should get the effect you want.
The z-order of the subviews was correct in my case. Depending on the color the effect I want is possible or not. So I made the following:
Choose another background color for the lavender color rectangles and adapt the alpha transparency to look similar. So something like this:
rectangle.BackgroundColor = UIColor.FromRGBA (245/255.0f,227/255.0f,249/255.0f,0.7f);

iOS how to make subview of a transparent view opaque?

I have a view as the subview of another view, then I set the alpha value of the father view something like 0.5, but this also makes the subview transparent even when its alpha value is set to 1. So how can I make the subview non-transparent(opaque) when its father view has an alpha value less than 1?
The closest you're going to get is colorWithAlphaComponent:. Using something like the following, you can set the alpha component of the parent view's background, and it won't affect subviews.
[yourSuperview setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];
If you set the parent view to 0.5 all the subviews will also go to 0.5 or less. You'll have to come up with a different design approach.
For Swift 4.2
self.yoursuperview.backgroundColor = UIColor.black.withAlphaComponent(0.5)
The following method worked for me.
superView.backgroundColor = .clear
Now that alpha property of superview is not altered, it won't affect the alpha of its subview. Hence, subviews will be opaque(if you have a background color for it).
You should have two designs for the parent view. One would be the "enabled" design, showing it as it is and the other would be the "disabled" design, which would contain transparent background.
You cannot do this with alpha, as subviews inherit the alpha. If
parent.alpha = x
and
view.alpha = y,
then the real alpha of the view will be:
x * y
I think, You have create View by Storyboard. I had already face this problem. Find the below code, this is correct in my case.
[MyParentView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6]];
Hope, This may help you.

Using UIColor tiled images as the background of a view doesn't work with cornerRadius

I have used UIColor colorWithPatternImage to get a tiled color. I set this as the background of my view. After this, any changes to view.layer.cornerRadius do not have any affect: it doesn't change the corner radius. I have also tried adding another subview to my view, setting that's background color to the pattern and using cornerRadius on my view to no avail.
How can I fix this behaviour?
Thanks for your time.
Set the clipsToBounds of the view to YES and it should work. I had the same issue.
Here is my snippet:
self.leftPanelView.layer.cornerRadius = 10;
self.leftPanelView.clipsToBounds = YES;
self.leftPanelView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"linePattern_gray.png"]];
which shows the linePattern_gray.png as the background patterned with a nice rounded corner.

Resources