Can't set the same color for background and border in UISegmentedControl - swift

I am trying to customize my segmented control with Swift, but I have run into a problem. I set the same color (e.g. UIColor.red) for border and background, but they look different. My code is:
segmentedControl.layer.cornerRadius = 12
segmentedControl.layer.borderWidth = 2
segmentedControl.layer.borderColor = UIColor.red.cgColor
segmentedControl.layer.masksToBounds = true
segmentedControl.backgroundColor = .red
Maybe someone knows how can I fix it?

Try setting the tintColor and selectedSegmentTintColor:
segmentedControl.tintColor = .white
segmentedControl.selectedSegmentTintColor = .white

Related

Can't set disabled UITextField background color to clear

In TvOS, I can't seem to get a UITextField to render with a clear background. Please note I am also using Xamarin as an intermediary.
var label = new UITextField()
{
Text = seperator,
UserInteractionEnabled = false,
BackgroundColor = UIColor.Clear,
VerticalAlignment = UIControlContentVerticalAlignment.Top
};
parent.AddArrangedSubview(label);
label.TranslatesAutoresizingMaskIntoConstraints = false;
label.HeightAnchor.ConstraintEqualTo(200).Active = true;
label.TextAlignment = UITextAlignment.Center;
The parent in this case is a UIStackView.
Here is the output:
It seems to work perfectly fine if you set it to any other color.
Any solutions would be greatly appreciated.

UISegmentedControl iOS 13 SelectedTint Default Color

I would like to match the default SelectedTintColor from the UISegmentControl to another view but I cannot find a matching color from the default system* colors provided in Attribute Inspector.
The background seems to be .quaternarySystemFill but there is no color which matches the selected color in both light and dark mode.
Even setting the color this way does not work since the .selectedSegmentTintColor returns nil:
label.backgroundColor = segmentedControl.selectedSegmentTintColor
Does anyone know the correct color or do I need to do this with a custom color?
Edit:
Edit 2:
Currently this line kind of works, but maybe there is a better way?
label.backgroundColor = label.traitCollection.userInterfaceStyle == .light ? .white : .tertiaryLabel
From my understanding of your question, you want to change the background color same as UISegmentControl tint color. What you need to do is simply use this:
label.backgroundColor = segmentedControl.tintColor
Hope this will help you
If you want to change the background same as the segment bar tint color you can try:
yourLabel.backgroundColor = segmentControl.tintColor.
I tried with
var myNormalFont = UIFont.FromName("OpenSans-SemiBold", 19f);
var myNormalTextColor = UIColor.LightGray;
AlertsSegmentedControl.SetTitleTextAttributes(new UITextAttributes() { Font = myNormalFont, TextColor = myNormalTextColor }, UIControlState.Normal);
var mySelectedFont = UIFont.FromName("OpenSans-SemiBold", 19f);
var mySelectedTextColor = UIColor.White;
AlertsSegmentedControl.SetTitleTextAttributes(new UITextAttributes() { Font = mySelectedFont, TextColor = mySelectedTextColor }, UIControlState.Selected);
AlertsSegmentedControl.BackgroundColor = UIColor.Clear;

View border Color not changing

I have a view I make border with this run time attributes:
The problem is layer.borderColor when I set borderColor my border is gone but when I don't set border Color I have a black border which I don't want
any ideas?
You are facing this issue because layer.borderColor want CGColor and from User defined runtime attributes you can only set UIColor not CGColor, when you don't set the color it will take default borderColor and i.e black color. To set borderColor you need to set it programmatically like this.
Swift 3
yourView.layer.borderColor = UIColor.red.cgColor //set your color here
Swift 2.3 or lower
yourView.layer.borderColor = UIColor.redColor().CGColor //set your color here
In addition to what #NiravD suggested, you need to set both borderColor and the borderWidth on the view's layer to see the border.
Swift 3 & Swift 4:
circle.layer.cornerRadius = 5.0
circle.layer.borderColor = UIColor.red.cgColor
circle.layer.borderWidth = 2.0
in Swift 3.0
#IBOutlet weak var viewMainCell: UIView!
viewMainCell.layer.shadowOpacity = 0.5
viewMainCell.layer.shadowRadius = 5
viewMainCell.layer.shadowColor = UIColor.black.cgColor
viewMainCell.layer.cornerRadius = 5
viewMainCell.layer.masksToBounds = false

MFMailComposeViewController navigationBar custom background color

I'm using MFMailComposeViewController and I'd like to change background color so it matches the one I have across the app. I've tried several things, but nothing worked(at least not on iOS 9).
let mailVC = MFMailComposeViewController()
mailVC.mailComposeDelegate = self
...
mailVC.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.whiteColor()] // this works
mailVC.navigationBar.tintColor = UIColor.whiteColor() // this works
mailVC.navigationBar.barTintColor = UIColor.blueColor() // this doesn't work
mailVC.navigationBar.backgroundColor = UIColor.blueColor() // this doesn't work
Background color stays default gray.
I solved it by setting color of navigationbar before initializing MFMailComposeViewController like this:
UINavigationBar.appearance().barTintColor = UIColor.blueColor()
let mailVC = MFMailComposeViewController()

SKLightNode: Changing alpha of ambientColor

Is there a way to change the alpha value of an SKLightNodes ambientColor so that e.g. their background can be seen even if the lightNode is on the other side of the screen?
//Add LightNode
let lightNode = SKLightNode()
lightNode.ambientColor = UIColor.blackColor()
lightNode.lightColor = UIColor.whiteColor()
lightNode.shadowColor = UIColor.blackColor()
lightNode.categoryBitMask = LightCategory.Light1.rawValue
lightNode.enabled = true
lightNode.falloff = 0.05
self.addChild(lightNode)
//Sprite
sprite.lightingBitMask = LightCategory.Light1.rawValue
It says here in the SKLightNode Class Reference that, "The alpha value of the color is ignored." If you want things on the other end of the screen to be visible, you should change the falloff.