반응형
Swift: iOS 화면 크기 결정
화면 크기에 상관없이 Swift 코드를 사용하여 앱에 항목을 올바르게 배치하고 싶습니다.예를 들어 버튼이 화면 너비의 75%가 되도록 하려면 다음과 같은 작업을 수행할 수 있습니다.(screenWidth * .75)단추의 너비가 됩니다.나는 이것이 목표-C에서 결정될 수 있다는 것을 발견했습니다.
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
유감스럽게도 이것을 Swift로 변환하는 방법을 잘 모르겠습니다.아이디어 있는 사람?
감사합니다!
Swift 5.0에서
let screenSize: CGRect = UIScreen.main.bounds
주의:UIScreen.mainiOS의 향후 버전에서는 더 이상 사용되지 않을 것입니다.그래서 우리는 사용할 수 있습니다.view.window.windowScene.screen.
스위프트 4.0
// Screen width.
public var screenWidth: CGFloat {
return UIScreen.main.bounds.width
}
// Screen height.
public var screenHeight: CGFloat {
return UIScreen.main.bounds.height
}
Swift 3.0에서
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
이전 swift에서:
다음과 같은 작업을 수행합니다.
let screenSize: CGRect = UIScreen.mainScreen().bounds
그러면 다음과 같이 너비와 높이에 액세스할 수 있습니다.
let screenWidth = screenSize.width
let screenHeight = screenSize.height
화면 너비의 75%를 원하는 경우 다음을 수행할 수 있습니다.
let screenWidth = screenSize.width * 0.75
언급URL : https://stackoverflow.com/questions/24110762/swift-determine-ios-screen-size
반응형
'codememo' 카테고리의 다른 글
| gitdiff에서 파일 제외 (0) | 2023.05.28 |
|---|---|
| Mac OS에서 Node.js를 최신 버전으로 업그레이드합니다. (0) | 2023.05.28 |
| 이벤트 이미터 메모리 누출이 감지됨 (0) | 2023.05.28 |
| 가시성 간의 차이입니다.축소 및 가시성.히든 (0) | 2023.05.28 |
| 줄 끝 변환이 깃 코어에서 작동하는 방식입니다.서로 다른 운영 체제 간의 자동 제어 (0) | 2023.05.28 |