По сути, я ни для чего не использую раскадровку и не пытаюсь создать все это программно. Я пытаюсь поместить изображение в качестве баннера в верхней части UIView, используя UIImageView. На рисунке показаны результаты.
Белый прямоугольник примерно вдвое длиннее того, что вы видите. У меня для contentMode установлено значение scaleAspectFit, и я думаю, что увеличение масштаба отталкивает его от левой стороны? Когда я создаю ярлык, он работает правильно, у меня просто возникают такие проблемы при масштабировании с использованием соотношения сторон.
РЕДАКТИРОВАТЬ: Добавил остальные мои классы, потому что, как указал Евгений, это, вероятно, как я настраиваю мой основной вид. Может быть, потому что, как я устанавливаю свои контроллеры как mainView в свойстве?
class MainView:UIView {
override init(frame: CGRect)
{
super.init(frame: frame)
self.backgroundColor = .green
setupViews()
setupConstraints()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews()
{
self.addSubview(banner)
let label = UILabel(frame: self.frame)
label.text = "HELLO WORLD"
self.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
label.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
label.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 50).isActive = true
label.rightAnchor.constraint(equalTo: self.leftAnchor, constant: 100).isActive = true
}
func setupConstraints()
{
let screenSize:CGRect = UIScreen.main.bounds
self.translatesAutoresizingMaskIntoConstraints = false
banner.translatesAutoresizingMaskIntoConstraints = false
banner.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
banner.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
banner.bottomAnchor.constraint(equalTo: self.topAnchor, constant: (screenSize.height/5) + 10).isActive = true
banner.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
}
let banner: UIImageView = {
let screenSize:CGRect = UIScreen.main.bounds
let image = UIImage(named: "serveBanner")
let iView = UIImageView(frame: CGRect(x:0, y:0, width:screenSize.width, height:(screenSize.height/5)))
iView.clipsToBounds = true
iView.contentMode = UIView.ContentMode.scaleAspectFit
iView.image = image
return iView
}()
}
MainViewController
import UIKit
class MainViewController: UIViewController {
var mainView:MainView {return self.view as! MainView }
var buttonClicked = false
private let navigator: MainNavigator
init(navigator: MainNavigator)
{
self.navigator = navigator
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView()
{
self.view = MainView(frame: UIScreen.main.bounds)
}
private func buttonAction()
{
navigator.navigate(to: .PipingSealsApp)
}
}
AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let navController = UINavigationController()
let mainNavigator = MainNavigator(navigationController: navController)
let mainViewController = MainViewController(navigator: mainNavigator)
navController.setViewControllers([mainViewController], animated: false)
window?.rootViewController = navController
window?.makeKeyAndVisible()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}