【发布时间】:2018-11-08 02:09:55
【问题描述】:
基本上,我没有将情节提要用于任何事情,而是尝试以编程方式创建这一切。我正在尝试使用 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
}()
}
主视图控制器
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:.
}
}
【问题讨论】:
-
像您发布的屏幕截图确实没有帮助,所以我没有放大它。你“尊重” `contentMode 吗?处理它?如果没有,请 - 粘贴到我们可能复制的问题代码(不是屏幕截图)中。谢谢。
-
不要真正欣赏否决票,因为您不喜欢屏幕截图,因为这无助于回答我的问题。我还将为那些想要的人添加代码块。至于 contentMode,它设置为 scaleAspectFit。所以我认为它正在远离左边缘但我不知道如何告诉它除了约束之外坚持左边缘。
-
向我们展示您的形象?
标签: ios swift uiimageview uiimage constraints