为什么将UIView作为.leftView重用于两个UITextFields会导致大量内存使用

时间:2018-09-20 23:43:32

标签: swift xcode

要在mkdir -Force cmake-vs | Out-Null Set-Location cmake-vs & "$Env:Programfiles\CMake\bin\cmake.exe" -G "Visual Studio 15 2017 Win64" ".." & "$Env:Programfiles\CMake\bin\cmake.exe" --build . ((Get-Content "myLib.csproj") ` <# add Sdk attribute to project which is needed by netstandard1.4 #> ` -replace ('<Project ','<Project Sdk="Microsoft.NET.Sdk" ') ` <# remove reference to ZERO_CHECK project which causes problems #> ` -replace ('(?ms)<ProjectReference.*</ProjectReference>','') | <# remove imports that are unneccessary for netstandard1.4 and cause problems #> ` Select-String -Pattern "Microsoft.CSharp.targets" -NotMatch | Select-String -Pattern "Microsoft.Common.props" -NotMatch | <# for some reason, Select-String prepends an empty line which is not allowed before <?xml ..., so we trim it away. #> Out-String).Trim() | Out-File "myLib.csproj" <# use dotnet util (part of the .NET Core SDK) to remove projects with problems from the solution because I am really tired of regexes #> & "$Env:Programfiles\dotnet\dotnet.exe" sln myLib.sln remove ALL_BUILD.vcxproj & "$Env:Programfiles\dotnet\dotnet.exe" sln myLib.sln remove ZERO_CHECK.vcxproj 上的 Swift 4.2 中创建填充效果,我使用了以下技术:

UITextField

这种方法可以正常工作,并且效果很好。

enter image description here

要重用import UIKit class BioController: UIViewController { @IBOutlet weak var nameTextField: UITextField! @IBOutlet weak var ageTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 27, height: self.nameTextField.frame.height)) self.nameTextField.leftView = paddingView self.nameTextField.leftViewMode = .always } } 并在第二个文本字段上获得相同的效果,我将代码更改为:

paddingView

我注意到的第一件事是控制器不再出现在(前控制器的)import UIKit class BioController: UIViewController { @IBOutlet weak var nameTextField: UITextField! @IBOutlet weak var ageTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 27, height: self.nameTextField.frame.height)) self.nameTextField.leftView = paddingView self.nameTextField.leftViewMode = .always self.ageTextField.leftView = paddingView self.ageTextField.leftViewMode = .always } } 上。在调查此问题时,我注意到:

enter image description here

记忆正在快速而艰难地爆发!删除self.present(并创建自己的视图即可解决此问题。但是,为什么这首先发生呢?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我不知道为什么这会导致您正在使用的内存过多,但是我可以告诉您不要这样做。 UIView只能位于视图层次结构中的一个位置。您不能将UIView放在两个类似的位置,并且不应这样做。

“医生,当我这样做时,它会很痛!”

医生:“那就不要了!”