I am trying to print a uiview in a label. I don't know if I should put accessibilityIdentifier or not. When I did put accessiblitiyIdentifier in my whole app loaded but was all black.
#IBOutlet var pink: UIView!
#IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = pink
}
AccessibilityIdentifier can use for UI Automation scripts,
label.text = pink wont be compile
you can use label.addSubview(pink)
label.text is of type String and pink is of type UIView!, that shouldn't even compile?
Related
I'm just trying to simply set the background image of the button, but whatever I try it is doing nothing. I also put a UIImageView there to see if there's any issue with the image file, but the image view is set perfectly fine. Do I have to set something in the properties of the button?
Here the code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageToSet = UIImage(named: "3-1")
b1.setBackgroundImage(imageToSet, for: .normal)
imageView.image = imageToSet
}
#IBOutlet weak var b1: UIButton!
#IBOutlet weak var imageView: UIImageView!
}
These are my setting for the button:
In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on (If you set one of them 4).So when you try to use old behaviours like
setBackgroundImage(..)
setImage(..)
This functions not gonna effect on this button. So If you want to use old behaviours you need to set style Plain to Default
I added a label to my storyboard and did a CTRL drag to my ViewController.swift file, set as an Outlet and gave it a name but it's not clear to me how to print a variable such as the following to my label.
var userName = "Ted"
In Swift playgrounds I can run the following and it prints the variable just fine but I can't make it do that in the label/IBOutlet once I compile the app.
var userName = "Ted"
print(userName)
Here's my ViewController so far:
import UIKit
class ViewController: UIViewController {
var userName = "Ted"
#IBOutlet var userNameLabel: UILabel? = nil
override func viewDidLoad() {
super.viewDidLoad()
print(userName)
}
}
I'm using Xcode 9.3.1 and Swift 4.
Since your label is an outlet and it is connected through your storyboard, change:
#IBOutlet var userNameLabel: UILabel? = nil
to:
#IBOutlet var userNameLabel: UILabel!
Then in viewDidLoad, instead of print, set the label's text:
userNameLabel.text = userName
Every label have one property called text which is used to set/get value to/from label for example::
lblObj.text = "text you want to display"
print(lblObj.text) // output:: "text you want to display"
text property will only except string type, since your username is of type string you can assign it like below
userNameLabel.text = userName
I have a strange issue when using monospacedDigitSystemFont(ofSize:weight:)
I have one UISlider and one UILabel in my UIViewController. The Label is showing the current value of the slider + some description text. When changing the slider's value, the text of myLabel is shaking left and right a bit. I would expect the myLabel's text to not shake left and right, since I am using monospacedDigitSystemFont(ofSize:weight:).
This is my code:
import UIKit
class ExampleViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
#IBOutlet weak var mySlider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.font = UIFont.monospacedDigitSystemFont(ofSize: 15, weight: .bold)
}
#IBAction func sliderChanged(_ sender: UISlider) {
myLabel.text = String(format: "%.5f is the actual Value of the Slider", sender.value)
}
}
GIF about the jiggle:
Any suggestions? Am I missing something?
This is a horrible bug and it's caused by the monospaced font being bold. Setting the weight to regular solves this problem.
myLabel.font = UIFont.monospacedDigitSystemFont(ofSize: 15, weight: .regular)
If you still want to use a bold font, consider using a non-standard monospaced font.
I have a custom UITableViewCell and one of its subviews is a button (with the title "RSVP"):
It is connected to the following code:
class SelectedEventsTableViewCell: UITableViewCell {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var descriptionLabel: UILabel!
#IBOutlet weak var attendanceLabel: UILabel!
#IBOutlet weak var attendanceButton: UIButton!
}
I am trying to change the background color of the button by adding this code to the SelectedEventsTableViewCell class:
override func awakeFromNib() {
super.awakeFromNib()
attendanceButton.backgroundColor = UIColor.greenColor()
}
However, it doesn't work. The button gets a white background:
Does anybody know why this is happening? It also doesn't work when I set the button's background color programmatically when the button is pressed.
Your help is appreciated.
Check somewhere you set the button background color white.
attendanceButton.backgroundColor = UIColor.whiteColor()
Check where you use SelectedEventsTableViewCell's object in TableView's data source or delegate methods in viewController.
ex.
cell.attendanceButton.backgroundColor = UIColor.whiteColor()
There are several possible reasons.
1.Did you make that outlet accessible? If not, CTRL-DRAG from storyboard.
2.Did you change that after awakeFromNib()? Maybe you set it again in cellForIndexPath(table view data source).
3.Cell was reused but awake once. It means you should recover its state after reuse. prepareForReuse() is designed for this.
I know this issue is old, but I ran into the same thing on iOS 15. The solution is to use the new UIButton.Configuration struct.
if #available(iOS 15.0, *) {
var config = UIButton.Configuration.filled()
config.baseBackgroundColor = UIColor.greenColor()
attendanceButton.configuration = config
} else {
attendanceButton.backgroundColor = UIColor.greenColor()
}
Run something like that in your prepareForReuse function or after you dequeue the cell and it should start working again. There are a lot more customisation options in iOS 15 using that configuration struct so have a dig into it for configuring all other aspects of your buttons.
I'm new in programming and I'm stuck with this little problem. I created a table view with several items, that passes data to a label and an image view through a segue. It all works fine, but now I want to make the image clickable, in order to segue to another view controller to show this image expanded. How can I do that?
class ViewController: UIViewController {
#IBOutlet var titleLabel: UILabel!
#IBOutlet var descLabel: UILabel!
#IBOutlet var imageView: UIImageView!
var titleName: String?
var descName: String?
var imageName: String?
func configureView() {
if let poster = self.imageName {
if let imagePoster = self.imageView {
imagePoster.image = UIImage (named: poster)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = titleName
descLabel.text = descName
self.configureView()
}
The easiest thing to do is to use a UIButton instead of an image view. Set it's type to custom and install the image into the button in IB (Interface Builder).
That way you can trigger an IBAction just like any other button. It also highlights on a touch like you'd expect, triggers on touch up rather than touch down, etc.
If you don't want to use a button-with-image, you have to set userInteractionEnabled = YES on the image view and install a tap gesture recognizer on it. See the docs on UITapGestureRecognizer for more information.