UIScreen.main is deprecating. What should I replace it with? - xcode

I'm using swiftUI and I want the width and height of the screen.
It says .main will be depreciated. What should I replace it with? What's the new approach?
I have this code in my project
import SwiftUI
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
I declared this in a global file.
I'm using iOS 16.2 (the latest xcode & iOS)

Related

PySide2 macOS QPushButton looks different while using different fonts, how can I fix this?

I started to learn and use PySide2 and Qt about 2 weeks ago. I am working on a personal project based on PySide2 and I am trying to add a change font feature.
I found that the frame of QPushButton looks different while I am using some fonts.
No matter it is dark mode or light mode, the buttons look different. When I using fonts like DejaVu Sans Mono for Powerline, it is the macOS default looking, but when I change it to fonts like JetBrains Mono, it becomes another style. Other controls do not have this problem.
Both fonts are based on ttf files and I do not figure out the relationship between the looking and the font.
What could be the reason and how can I keep the macOS default looking no matter what font I use?
Some information about the system and environments:
macOS Big Sur 11.3.1
Python 3.9.4
PySide2==5.15.2
And here is the source could reproduce this problem.
import os
import sys
os.environ["QT_MAC_WANTS_LAYER"] = "1" # https://stackoverflow.com/a/66204842/1166461
from PySide2 import QtGui
from PySide2 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
fonts = QtWidgets.QComboBox(window)
fonts.insertItems(0, QtGui.QFontDatabase().families(QtGui.QFontDatabase.Latin))
fonts.currentIndexChanged.connect(lambda i: QtWidgets.QApplication.instance().setStyleSheet(f'''
* {{
font-family: "{fonts.itemText(i)}";
}}
'''))
layout.addWidget(fonts)
layout.addWidget(QtWidgets.QPushButton('I am a push button', parent=window))
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
Finally, I figured out the reason by myself. Leave this answer here for someone might be confused by the same problem.
The reason that the button frame changed is not the font but the size, or the height of the button. If the button's height is more than 30px, the frame will change.
In this document about QPushButton, it says:
Note
On macOS when a push button’s width becomes smaller than 50 or its height becomes smaller than 30, the button’s corners are changed from round to square. Use the setMinimumSize() function to prevent this behavior.
But actually, according to my experiments, the width does not affect the shape of the button, only when the height becomes bigger than 30, the button will change to square frame.
As a solution, I add this css to keep it to the round style.
QPushButton {
max-height: 30px;
}
So the complete source become
import os
import sys
os.environ["QT_MAC_WANTS_LAYER"] = "1" # https://stackoverflow.com/a/66204842/1166461
from PySide2 import QtGui
from PySide2 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
window.setLayout(layout)
fonts = QtWidgets.QComboBox(window)
fonts.insertItems(0, QtGui.QFontDatabase().families(QtGui.QFontDatabase.Latin))
fonts.currentIndexChanged.connect(lambda i: QtWidgets.QApplication.instance().setStyleSheet(f'''
* {{
font-family: "{fonts.itemText(i)}";
}}
QPushButton {{
max-height: 30px;
}}
'''))
layout.addWidget(fonts)
layout.addWidget(QtWidgets.QPushButton('I am a push button', parent=window))
window.show()
sys.exit(app.exec_())

How to give width to the TabViewItem in Nativescript?

I want to implement sometthing like above where the camera tab has the smaller width compare to the others tab in TabView.
I did three tabs with same size but I don't know how to add one more tab but with smaller width.
You will have to update the width of native view that contains the camera within the tab layout. The code below lets the layout take only the width it requires to show the content within.
const view = nativeTabView.getChildAt(i),
layoutParams = view.getLayoutParams();
layoutParams.weight = 0;
layoutParams.width = android.widget.LinearLayout.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(layoutParams);
Playground Sample
The above example is written in NativeScript Core / TypeScript, there is nothing you have to specifically if you are implementing this on Vue / Angular, just use the appropriate event binding syntax, you should be good.

Set width of Switch

I am using Nativescript with Angular and I would like to set the width of a Switch element.
To do this I added:
style="width: 20%; min-width: 20%;"
To the element in the template.
But the width of the element doesn't change when I test it.
Why isn't it working?
Behind the NativeScript's Switch we have android.widget.CompoundButton on Android and UISwitch on iOS. There is no option to change the width of the Switch widget but you can access the native element and modify its native width (if possible). As far as I've seen modifying the width of UISwitch is as simple as marshaling the below code to JavaScript
aSwitch.transform = CGAffineTransformMakeScale(2.0, 2.0);
In NativeScript you can access the native view (iOS or Android) via nativeView property.
For example (TypeScript):
let mySwitch= <Switch>page.getViewById("mySwith");
if (isIOS) {
let iosSwitch = mySwitch.nativeView;
iosSwitch.transform = CGAffineTransformMakeScale(2, 2);
}
Following the same principle, you could adopt a native Android solution to adjust the Android counterpart.

MonoDevelop window won't shrink

The MonoDevelop window will not get any smaller than the screenshot, posted. I am using it on a different computer than I have before (now using Windows 8.1), and have not encountered this problem until now (old computer was on Vista). I tried using Window's "drag to the side and snap to half the screen" feature. All it does is match the height with the screen height. The width remains obnoxiously large. Please, help.
The smallest that MonoDevelop will go
The min. width and height of the Workbench/IDE is:
const int MinimumWidth = 800;
const int MinimumHeight = 540;
You would need change it in source and re-compile MonoDevelop.
Located in DefaultWorkbench.cs

How do I position my game over menu so it looks the same in every iOS device on Swift?

I'm making a game for the iPhone and iPad and I don't understand how when I position my menu on the iPhone 5s it looks different on the iPad. Like my score it is set perfectly in the score menu on the 5s and 4 and 5 but in the iPad its a little lower and not the same positioning. How do I fix this? Thanks! I use this code to position my menus:
endOfGameHighScoreLabel.position = CGPointMake(self.size.width / 1.8,
self.size.height / 2.7)
ahm with self.size.width you don't get the size of the view but the label?
I would recommend to create a view containing all the game over stuff you need and then position the center of it in the view's center:
gameOverView.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/2)

Resources