adjustsFontSizeToFitWidth not working with iOS 15 UIButton - swift

I want to size the text on my button to automatically fit to the edges of the button.
This code was working pre-iOS15, now it doesn't.
testButton.titleLabel?.adjustsFontSizeToFitWidth = true
testButton.titleLabel?.minimumScaleFactor = 0.1
How can I automatically resize the text to fit the size of my button in iOS 15?
This is playground code for testing. It includes everything I've tried, but still isn't working.
import UIKit
import PlaygroundSupport
//building button
let testButton = UIButton(frame: CGRect(x: 100, y: 100, width: 300, height: 300))
testButton.setTitle("test", for: .normal)
//everything I've found in other answers that is supposed to work
var titleLabel = testButton.titleLabel!
titleLabel.font = UIFont.systemFont(ofSize: 300)
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.1
titleLabel.numberOfLines = 1 //also tried 0 instead
titleLabel.lineBreakMode = .byClipping
//configuring button
testButton.configuration = UIButton.Configuration.filled()
//display the button
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
containerView.backgroundColor = .white
PlaygroundPage.current.liveView = containerView
containerView.addSubview(testButton)
Related questions I've already looked at and tried:
Best way to adjust font size with width and height of UILabel
Swift - Adjusting fontSize to fit the width of the layout (programmatically)
How to get .adjustsFontSizeToFitWidth to function properly
How to set font size to fill UILabel height?
Auto change the font size to fit the button in swift
UIButton auto-adjust Button font size Swift

I found out it could be caused by testButton.configuration = UIButton.Configuration.filled(), but I don't know why.

Related

UIImage not resizing according to UIImageView size width and height

I am a Swift newbie and I am trying to add an UIImageView on the right of UITextField using Storyboard and #IBInspectable annotation.
However, when I define my UIImageView with specific size and add the UIImage, the UIImage is not resizing according to UIImageView size.
Here's my code:
#IBInspectable var rightImage: UIImage? {
didSet {
guard let image = rightImage else {
return
}
rightViewMode = .always
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = image
imageView.contentMode = .scaleAspectFit
rightView = imageView
and here's the result on the screen:
Big big icon
If you can tell me what is wrong with my code, do not hesitate.
Thanks.
I use constraints based on Matt’s suggestion in the comment, and it's working now:
let constraints = [
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
]
NSLayoutConstraint.activate(constraints)

Centering buttons programatically in Swift

I am trying to center Google Sign In and Sign Out buttons programmatically. In order to put both of them in the third quarter. I create 2 views that I wanted to put buttons to their center, in Storyboard.
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance().signIn()
let gSignIn = GIDSignInButton(frame: CGRect(x: 0, y: 0, width: loginView.frame.size.width, height: loginView.frame.size.height))
loginView.addSubview(gSignIn)
gSignIn.center = loginView.center
let gSignOut = UIButton(frame: CGRect(x: 0, y: 0, width: signOutView.frame.size.width, height: signOutView.frame.size.height))
gSignOut.backgroundColor = UIColor.white.withAlphaComponent(0)
gSignOut.setTitle("Sign Out", for: .normal)
gSignOut.setTitleColor(UIColor.red, for: .normal)
gSignOut.addTarget(self, action: #selector(self.signOut(_:)), for: .touchUpInside)
gSignOut.center = signOutView.center
self.signOutView.addSubview(gSignOut)
As you can see, also I am trying to resize buttons according to view size which helps me to resize buttons depend on device size.
Here is the second half of my storyboard.
Here is the simulator screen when I run the code.
Thanks.
Use auto-layout instead of setting frame.center, it will be more flexible.
Autolayout goes like below code.
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance().signIn()
let gSignIn = GIDSignInButton(frame: CGRect(x: 0, y: 0, width: loginView.frame.size.width, height: loginView.frame.size.height))
gSignIn.translatesautoresizingmaskintoconstraints = false
loginView.addSubview(gSignIn)
NSLayoutConstraint.activate([
gSignIn.leftAnchor.constraint(equalTo: loginView.leftAnchor),
gSignIn.rightAnchor.constraint(equalTo: loginView.rightAnchor),
gSignIn.topAnchor.constraint(equalTo: loginView.topAnchor),
gSignIn.bottomAnchor.constraint(equalTo: loginView.bottomAnchor)
])

set background color of footerView UI TableView doesn't work

I use UIView() for remove separate line and it works but changing background color of footer view doesn't work. This is my code. Can somebody explain why? Thank you so much!
let footerView = UIView()
footerView.backgroundColor = UIColor(hexString: "#F7F9FC")
myTableView.tableFooterView = footerView
It is because the size of your footerView is zero.
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40.0))
footerView.backgroundColor = UIColor(hexString: "#F7F9FC")
myTableView.tableFooterView = footerView
I think you must set a frame for footer view. If you just only create UIView then the default of Apple will create UIView with frame is zero. You need to set width, height, x, y for it to display in TableView
let frame = CGRect(x: 0, y: 0, width: yourWidthExpect, height: yourHeightExpect))
let footerView = UIView(frame: frame)
After that you can set background color and do anything.

How do I add a background to a scroll view and have it repeat to the end?

I am making a roster on a scroll view with every slide being someone different. I was wondering if there is a way to set a background and have it repeat to the end as it cuts off at a certain person
Currently, I am copy and pasting the background and dragging it to match
for i in 0..
let label = UILabel(frame: CGRect(x:xPosition+110, y: 350, width: 200, height: 200))
label.text = names[i]
label.font = UIFont.systemFont(ofSize: 35, weight: UIFont.Weight.bold)
label.adjustsFontSizeToFitWidth = true
label.textColor = UIColor.black
let imageView = UIImageView()
imageView.image = UIImage(named: names[i])
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect(x: xPosition-110, y: 150, width: 650, height: 250)
let descriptionM = UITextView(frame: CGRect(x: xPosition+65, y: 525, width: 300, height: 250))
descriptionM.text = desc[i]
descriptionM.textColor = UIColor.black
descriptionM.backgroundColor = UIColor.clear
descriptionM.contentMode = .scaleAspectFill
descriptionM.font = UIFont.systemFont(ofSize: 20)
scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
scrollView.addSubview(label)
scrollView.addSubview(imageView)
scrollView.addSubview(descriptionM)
}
Results would be the photo to repeat to the end and past.
One way is to turn the background image into a resizable image with zero edge insets and the default (tiling) resizing mode. Set your image view's contentMode to .scaleToFill.
let image = UIImage(named: "background")!.resizableImage(withCapInsets: .zero)
imageView.contentMode = .scaleToFill
imageView.image = image
Then make sure your imageView is at least as big as your scroll view's contentSize.

Align Drop Down Menu Title to prevent overlapping of other buttons

I have the below image where the title of the drop down menu title is overlapped with other buttons. How to solve this issue?
Code:
func createDropDownMenu() {
// create the drop down menu
let title = prepareNavigationBarMenuTitleView()
prepareNavigationBarMenu(title)
updateMenuContentOffsets()
}
func prepareNavigationBarMenuTitleView() -> String {
// Both title label and image view are fixed horizontally inside title
// view, UIKit is responsible to center title view in the navigation bar.
// We want to ensure the space between title and image remains constant,
// even when title view is moved to remain centered (but never resized).
titleView = DropDownTitleView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
titleView.addTarget(self,
action: #selector(DocumentViewController.willToggleNavigationBarMenu(_:)),
for: .touchUpInside)
titleView.addTarget(self,
action: #selector(DocumentViewController.didToggleNavigationBarMenu(_:)),
for: .valueChanged)
titleView.titleLabel.lineBreakMode = NSLineBreakMode.byClipping
titleView.titleLabel.numberOfLines = 2
titleView.titleLabel.textColor = UIColor.black
titleView.title = currentNode.title
navigationItem.titleView = titleView
return titleView.title!
}
I had to set the frame of the TitleLable and set the numberOfLines = 0 to solve my problem.
Code:
titleView.titleLabel.frame = CGRect(x: 0, y: 0, width: 600, height: 80)
titleView.numberOfLines = 0
titleView.titleLabel.text = currentNode.title