ios - UIImageView 不遵守约束

标签 ios swift uiimageview uiimage constraints

基本上我没有使用 Storyboard来做任何事情,而是试图以编程方式创建这一切。我正在尝试使用 UIImageView 将图像作为横幅放在 UIView 的顶部。图为结果。

白色矩形大约是您看到的长度的两倍。我将它的 contentMode 设置为 scaleAspectFit 并且我认为放大是将它推离左侧?当我创建一个标签时它工作正常,我只是在使用宽高比缩放时遇到这样的问题。

编辑:添加了我的其余类(class),因为正如 Evgeniy 指出的那样,这可能是我设置主视图的方式。可能是因为我如何将我的 Controller 自身设置为属性中的 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
}()

主视图 Controller

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)
}
}

应用委托(delegate)

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:.
}
}

Code Results

最佳答案

因此,在知道它应该有效后进行挖掘后,我制作了另一个试验台并找到了解决方案。这实际上只是一些愚蠢的事情,但人们应该意识到这一点。此代码应删除 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
}

我不完全确定为什么会这样,但我会仔细阅读该属性(property),看看我是否能理解它。感谢所有的帮助!

关于ios - UIImageView 不遵守约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53200566/

相关文章:

ios - 如何从服务中设置图像并显示在表格单元格中?

ios - 为什么在 UIScrollView 中看不到 UIStackView?

ios - 表达式类型不明确,没有更多上下文 : . UIApplication.willResignActiveNotification

swift - 创建一个类似于 Apple 的焦点方 block 的对象放置指示器

iphone - UIScrollView 使用系统中的两个 uiimageview 初始化?

ios - SpriteKit - 接触错误

ios - 编辑时tableView中没有didSelectRowAtIndexPath()

ios - 在 addSubview 期间奇怪地复制 UIView

ios - 如何使用 UIKit 更快地渲染具有效果的图像

swift - 以编程方式自动布局各种尺寸的图像数组