基本上,我没有使用情节提要,而是尝试以编程方式创建所有提要。我正在尝试使用UIImageView将图像作为横幅放在UIView的顶部。图为结果。
白色矩形大约是您看到的两倍。我将其contentMode设置为scaleAspectFit,并且我认为按比例放大会将其从左侧移开?创建标签时,它可以正常工作,但是在使用宽高比进行缩放时只会遇到类似的问题。
编辑:添加了我的其余课程,因为正如Evgeniy指出的那样,这可能是我设置主视图的方式。也许是因为我如何在属性中将控制器自身设置为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:.
}
}
答案 0 :(得分:1)
因此,在得知应该工作之后,我做了另外一个试验床并找到了解决方案。这实际上只是愚蠢的事情,但人们应该意识到这一点。此代码应删除self.translatesAutoresizingMaskIntoConstraints = false
。
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
}
应阅读
func setupConstraints(){
let screenSize:CGRect = UIScreen.main.bounds
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
}
我不太确定为什么会这样,但是我会仔细阅读该属性,看看是否可以理解。感谢您的所有帮助!
答案 1 :(得分:0)
我尝试过对您的代码进行任何修改(好,图像名称除外),并得到以下信息:
这是您要实现的目标吗? 如果是,那么问题可能出在如何将主视图添加到视图控制器上? 这是我的代码,请比较:
let mainView = MainView(frame: self.view.bounds)
mainView.backgroundColor = .green
self.view.addSubview(mainView)
mainView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
mainView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
mainView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
mainView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true