Can't set disabled UITextField background color to clear - swift

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.

Related

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

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

Setting a variable to a defined lazy var within a UIView closure cause reference problems

Recently, I was working on a project of mine and I wanted to have multiple labels with the same font, text color, and properties, except their text.
This is the code I wrote:
lazy var profileLabel: UILabel = {
let label = UILabel()
label.font = .displayNameLabel
label.textColor = .profileLabel
label.numberOfLines = .numberOfLines
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var displayName: UILabel = {
let label = profileLabel
label.text = "Kevin"
return label
}()
lazy var countryLabel: UILabel = {
let label = profileLabel
label.text = "US"
return label
}()
As you can see, to remedy my issue, I created a single label that had all the properties I wanted for all my other labels. For my other labels, I thought I was creating a new label by typing let label = profileLabel. But as it turns out, I wasn't. After consecutive calls of setting the text and adding the labels to my view, only 1 label was actually displayed, and it was the last label added; so in this case, it would be the countryLabel.
It seems to me that in all my calls to let label = profileLabel, I'm just creating a reference to the same profileLabel. And if this is the case, would changing lazy var profileLabel to var profileLabel fix this issue and create a new label with the needed properties every time profileLabel is called?
You were intended to use computed property of swift. But didn’t get it right. Your profile label should have been defined as follows.
var profileLabel: UILabel {
get {
let label = UILabel()
label.font = .displayNameLabel
label.textColor = .profileLabel
label.numberOfLines = .numberOfLines
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}
}

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;

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()

Swift, Change UIButton color, by tag value

Im trying to change a UIButtons color, using tag value, I have 2 different codes, but none of them works.
self.view.viewWithTag(Int(buttonNumber))?.backgroundColor = UIColor.blueColor()
var tempButton = self.view.viewWithTag(Int(buttonNumber)) as? UIButton
tempButton?.backgroundColor = UIColor.blueColor()
None of theese works, and i can't find out how to solve this
This worked for me:
var tempButton = self.view.viewWithTag(Int(buttonNumber)) as? UIButton
tempButton?.backgroundColor = UIColor.blue.withAlphaComponent(0.6)
I hope it helps