CGRectIntersection setting colour - ios

I'm using this code here to create a view with a transparent area.
https://github.com/heigong/PartialTransparentMaskView/blob/master/src/PartialTransparentMaskView.swift
The issue is that the intersection is black even though the colour is set to UIColor.clearColour()
My code for creating this view is below
let view = PartialTransparentMaskView(frame: self.view.bounds, backgroundColor: UIColor.blueColor(), transparentRects: [self.testView.frame], transparentCircles: nil)
self.view.addSubview(view)
How this looks is also below
The area in the black is set to UIColor.clearColor() so I genuinely have no idea why it would be appearing black.
Any help would be appreciated thanks.

Related

UIImageView tintColor colors whole image

I've tried changing the tintColor of a UIImageView inside a UICollectionViewCell after a tap by setting render mode of the icons to Template Image and then setting the color by calling
iconView.tintColor = .blue
Problem: This fills the whole image with the chosen color, Instead of just changing the non-transparent parts.
This method has been working for me in the past, and I'm unable to figure out the problem. I assumed that the icons might have a white background instead of a transparent one, but that's not the case. Maybe it has something to do with the UIImageView being inside a UICollectionViewCell?
Help would be highly appreciated!
try this
imageViewHome.image = UIImage.init(named: "yourImageName")?.withRenderingMode(.alwaysTemplate)
imageView.tintColor = .blue
The following code will color only the non transparent area.
yourImage.image = yourImage.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
yourImage.tintColor = UIColor.redColor()
Happy coding.

Swift UIView Opacity Programmatically

I'm having quite a hard time messing with UIView's properties to achieve a desired opaque look programmatically that can be easily done through Storyboard and Attributes Inspector.
What I want to re-create (settings from attributes inspector):
A UIView with a background color of (RGB Sliders: 0, 0, 0) and Opacity Slider set at 75%, with alpha defaulted at 1. So basically a black UIView with the opacity toned down.
What I've tried programmatically:
1) view.backgroundColor = .black
view.alpha = 0.75
2) view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
view.isOpaque = true
Attached is a picture of the UIView selected in Storyboard with the settings for you to see. If you need any more information, please don't hesitate to let me know. Much thanks for your help.
UPDATE: Thanks for all your input. The combination of Clever Error's explanation of view layers and Matt's code allowed for me to achieve my desired look.
Desired UI Look!
What you are describing is:
view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
view.isOpaque = false
The view must not be marked opaque, as it is not opaque.
You can set opacity to view like below in Swift 3
view.alpha = CGFloat(0.1)
Summarizing these answers https://stackoverflow.com/a/40405424/7767664, https://stackoverflow.com/a/46669162/7767664
for UIView itself opacity
view.isOpaque = false
view.alpha = CGFloat(0.5)
for UIView Background opacity
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
view.isOpaque = false
When your app runs, the view hierarchy will be
UIWindow
UINavigationControllers view
Your View Controllers View
The problem is that UIWindow is black by default, and the UINavigationControllers view is clear. So it ends up mixing your transparent black color with full black to get just black.
You would need to add another view to your view controller that is transparent black then set the view controllers view color to the one you want to blend with.

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

remove searchbar black border

I have a search bar that looks like this:
Is there anyway to remove that black border on the top?
I tried the minimal bar approach but got:
So the border was removed but now I would need to somehow color the inner field white.
I tried:
if let searchTextField = searchBar.valueForKey("searchField") as? UITextField {
searchTextField.backgroundColor = UIColor.whiteColor()
}
But it didn't seem to change anything.
I'm looking for a solution to either of these problems. I just want the pink border with the white text field.
Edit:
There is a similar question for iOS7 but the accepted answer did not work for me.
Creating an image filled with the pink color:
And setting it as the backgroundImage for the search bar did the trick.

CollectionView background clearColor not working

I'm developing a small collectionview 'framework' to behave like a browser tab bar (think chrome) on the iPad. The code is all done, custom flow layout, reordering, and so on and is organized as so :
• TabBarCollectionViewController .h/.m/.xib contains the high logic of the collection view (delegates + datasource methods). I have the xib to configure the collectionView settings and set the custom flow layout (I could do this programmatically, but oh well it's easier that way).
• CustomFlowLayout .h/.m (subclass of flow layout)
• TabBarCell .h/.m/.xib (subclass of collectionviewcell)
Then I'm adding the TabBarCVC as a childViewController on my main viewController (this viewController has many childViewController and subviews) and then as a subview.
At this point all is working fiiiiine.
Now the problem, it's so stupid i can't believe i haven't found a way to do this, the backgroundColor of the collectionView is not settable to clearColor. I can put it in gray or whatever color, but that it doesn't support transparency. The cell background color is also clear and does work.
I need the collectionView to be transparent to show the texture on the main view behind. Any insight would be much appreciated, or perhaps i'll fill my first radar to apple.
If i can't find any solution i'll just add the 'screenshot' of the texture supposed to be behind the collectionView and add it as a imageView in the collectionView's backgroundView.
In my case I had the background color in the storyboard set to Default. This caused it to have a black background. Changing it to Clear Color worked.
Try setting the color to clear and the background view to an empty view like so...
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Ok so i'm feeling pretty stupid now. I left an empty UIView behind, acting as a container for the collectionView for a test. I simply forgot to remove it, all is working fine with a nice clearColor now...
Watch out when setting UICollectionViews Background Color in Storyboard:
The initially selected value Default is Black (counterintuitively).
You have to explicitly select Clear Color for the View to be transparent.
Also note that the Preview in Storyboard immediately changes when this is done 'right'...
The easiest solution is just pick anything color in color picker to change collectionview background then turn the opacity to 0%.
I solved it using in Swift 3:
collectionViewVideo.backgroundColor = UIColor.clear.withAlphaComponent(0)
Fogmeister's answer worked great. Adapted to Swift 3, it would be:
self.collectionView.backgroundColors = [NSColor.clear]
self.collectionView.backgroundView = NSView.init(frame: CGRect.zero)
Swift 4.0 from Fogmeister's answer
self.collectionView.backgroundColor = UIColor.clear
self.collectionView.backgroundView = UIView.init(frame: CGRect.zero)
To have a nice semi-transparent white background use:
collectionView.backgroundColor = UIColor(displayP3Red: 1.0, green: 1.0, blue: 1.0, alpha: 0.35)
In swift this work with me:
self.collectionView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.0)
What is did to fix it
From Storyboard set collection view background colour as clear colour
then set main view colour to any colour you want , (I set to white.)

Resources