UIImageView 不尊重约束

Posted

技术标签:

【中文标题】UIImageView 不尊重约束【英文标题】:UIImageView not respecting constraints 【发布时间】: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。所以我认为它正在远离左边缘但我不知道如何告诉它除了约束之外坚持左边缘。 向我们展示您的形象? 【参考方案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

我不完全确定为什么会发生这种情况,但我会阅读该物业,看看我是否能理解它。感谢大家的帮助!

【讨论】:

我发现了类似的东西。如果selfUIViewContoller.view 或者它的根视图,似乎将自动调整大小掩码设置为false 会搞砸子视图。 次我在根视图上使用它,但很少(通常不是)。老实说?我不知道为什么,但我的猜测UIWindowUIScreen有关,这两者可能在操作系统中都没有使用任何东西.很高兴你发现了你的问题。【参考方案2】:

我试过你的代码没有任何修改(好吧,除了图像名称),得到了这个:

这就是你想要达到的目标吗? 如果是,那么问题可能在于您如何将主视图添加到视图控制器上? 这是我的代码,请比较:

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

【讨论】:

在导航栏的情况下,顶部和底部锚点可以是mainView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = truemainView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true 感谢您的信息!我仍然觉得奇怪的是文本标签会找到正确的约束但 UIImageView 没有?我添加了其他课程以帮助提供更多见解。我正在创建一个 UINavigationController 并将我的 ViewController 添加为它的第一个元素。在视图控制器中,我设置了一个属性以将我的 MainView 作为 self.view 返回。这可能是问题所在? 我终于能够重现该问题。导航控制器真的坏了。从setupConstraints() 中删除这行self.translatesAutoresizingMaskIntoConstraints = false,你应该没问题。我只能假设,但不能自信地解释它为什么会发生。如果有人澄清这一点,那就太好了。 是的,我发现了同样的事情。我很想了解它为什么会发生。最奇怪的部分是标签正常执行,而图像却没有。因此,它不仅导致出现问题,而且前后不一致。这让我觉得这是一个错误。

以上是关于UIImageView 不尊重约束的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView 中的 UIView 尊重一些约束,但不尊重其他约束

查看不尊重约束,这是为啥呢?

ScrollView 中的视图不尊重约束

动画 UIView 框架不尊重约束?

如何在 QuickDialog 中将 UIImageView 添加到 Qsection?

如何让 UIImageView 尊重它的 superview.layer.mask