当前位置: 首页 > 面试题库 >

可以修改UISearchbar TextField的高度吗?

晏弘雅
2023-03-14
问题内容

我正在测试自己的用户界面,发现搜索栏有点过窄,无法满足我的需求。我还想确保视力较差或手动灵活性较差的人在调配所需界面时没有问题。

所以,我想做的是调整UITextfield内部的高度UISearchbar

我尝试过的操作:1.在情节提要中,添加“ UISearchbar高度限制”-结果:搜索栏大小增加,但UITextField内部保持不变。

  1. 访问UITextField内部UISearchbar并修改其高度-结果:控制台输出显示参数已修改,但是在屏幕上,UITextField高度保持不变。

注意-我可以修改UITextField使用方法2的其他参数,并且更改会反映在屏幕上,因此我知道UITextField

方法2的代码放在UISearchbar所在的ViewController的viewDidLoad()中:

for tempView in (self.roomInfoSearchBar.subviews[0] as! UIView).subviews as! [UIView]
{
  if let tV = tempView as? UITextField
  {
    var currentFrameRect = tV.frame
    currentFrameRect.size.height = 80
    //tV.layer.backgroundColor = UIColor.blueColor().CGColor  //This works fine
    //tV.layer.cornerRadius = 5  //This works fine
    //tV.font = UIFont(name: "Courier", size: 40)  //This works fine
    println(tV.frame.height)  //console output:0.0
    tV.frame = currentFrameRect
    println(tV.frame) //console output:(0.0, 0.0, 0.0, 80.0)
    println(currentFrameRect) //console output:(0.0, 0.0, 0.0, 80.0) - redundant, just checking
    println(tV.frame.height) //console output:80.0 - it says 80, but screen shows searchbar definitely not 80.
  }
}

我认为这与自动布局有关,无论设置在哪里,它都可以忽略参数。我想继续使用自动版式,因为它为我做了很多工作,但是我希望它的行为就像在Storyboard中一样,在该版面上,当用户设置设置时,它将在自动版式中使用这些参数,并在可以的时候抱怨。
t而不是忽略设置而没有反馈。

编辑:

为了回应马赫什。我不太了解客观的C ++,但是我尽力将您编写的内容转换为快速的内容。这就是我所拥有的:

(在我的viewcontroller.swift内部)

func viewDIdLayoutSubviews()
{
  super.viewDidLayoutSubviews()
  self.roomInfoSearchBar.layoutSubviews()

  var topPadding: Float = 5.0
  for view in (self.roomInfoSearchBar.subviews[0] as! UIView).subviews as! [UIView]
  {
    if let tfView = view as? UITextField
    {
      var calcheight = self.roomInfoSearchBar.frame.size.height - 10
      tfView.frame = CGRectMake(tfView.frame.origin.x, CGFloat(topPadding), tfView.frame.size.width, self.roomInfoSearchBar.frame.size.height - CGFloat(topPadding * 2))
    }
  }
}

由于在将您的代码转换为Swift时遇到了一些问题,因此我保留了用于访问文本字段的代码。对于CGRectMake-
swift抱怨各种类型,包括topPadding不是CGFloat,而对于最后一个变量(Y大小),它又不喜欢将CGFloat与Float混合,所以我也必须更改它。

不幸的是,它似乎不起作用。我在情节提要中将UISearchbar的高度更改为80,而我刚得到一个非常高的搜索栏,其文本字段覆盖了总高度的1/3。

编辑#2:合并Yuyutsu和更正的Mahesh代码版本。
仍不完美,但更接近。Yuyutsu的代码有效,但正如我的评论中所述,该字段不会居中,并且在首次加载视图时,调整大小也是可见的(从高度A跳到高度B)。另一个不足之处是,由于viewDidAppear方向改变后立即进行了大小调整,因此该字段返回到固有大小。

我基于Yuyutsu的代码:

  override func viewDidAppear(animated: Bool)
  {
    super.viewDidAppear(animated)
    var roomInfoSearchBarFrame = roomInfoSearchBar.frame
    var newHeight: CGFloat = 60
    for subView in roomInfoSearchBar.subviews
    {
      for subsubView in subView.subviews
      {
        if let textField = subsubView as? UITextField
        {
          var currentTextFieldFrame = textField.frame
          var recenteredY = (roomInfoSearchBarFrame.height - newHeight)/2
          var newTextFieldFrame = CGRectMake(textField.frame.minX, recenteredY, textField.frame.width, newHeight)

          textField.frame = newTextFieldFrame
          textField.borderStyle = UITextBorderStyle.RoundedRect
          textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
          //textField.backgroundColor = UIColor.redColor()
          //                    textField.font = UIFont.systemFontOfSize(20)
        }
      }
    }
  }

查看Yuyutsu的代码,我想知道还有什么地方可以进行此调整,并且通过另一个stackoverflow帖子遇到了链接。根据我的阅读,我看到马赫什的答案应该起作用。这就是我意识到为什么它不起作用的原因-我需要 重写func viewWillLayoutSubviews()

当前代码:保持尺寸随方向变化。 但是大小跳转仍然可见(不应该看到)

  override func viewWillLayoutSubviews()
  {
    super.viewWillLayoutSubviews()
    var roomInfoSearchBarFrame = roomInfoSearchBar.frame
    var newHeight: CGFloat = 60
    for subView in roomInfoSearchBar.subviews
    {
      for subsubView in subView.subviews
      {
        if let textField = subsubView as? UITextField
        {
          var currentTextFieldFrame = textField.frame
          var recenteredY = (roomInfoSearchBarFrame.height - newHeight)/2
          var newTextFieldFrame = CGRectMake(textField.frame.minX, recenteredY, textField.frame.width, newHeight)

          textField.frame = newTextFieldFrame
          textField.borderStyle = UITextBorderStyle.RoundedRect
          textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
          //textField.backgroundColor = UIColor.redColor()
          //                    textField.font = UIFont.systemFontOfSize(20)
        }
      }
    }
  }

因此,现在,剩下的唯一事情就是尝试使文本字段成为视图可见之前的设置大小,因此用户看不到任何大小偏移。

最终编辑:完全有效的代码 在Yuyutsu的附加帮助下,以下代码可以完成我想要的一切-从设置的大小开始,以字段为中心,并很好地处理旋转。

  override func viewDidLayoutSubviews()
  {
    super.viewDidLayoutSubviews()
    self.roomInfoSearchBar.layoutIfNeeded()
    self.roomInfoSearchBar.layoutSubviews()

    var roomInfoSearchBarFrame = roomInfoSearchBar.frame
    var newHeight: CGFloat = 60 //desired textField Height.
    for subView in roomInfoSearchBar.subviews
    {
      for subsubView in subView.subviews
      {
        if let textField = subsubView as? UITextField
        {
          var currentTextFieldBounds = textField.bounds
          currentTextFieldBounds.size.height = newHeight
          textField.bounds = currentTextFieldBounds
          textField.borderStyle = UITextBorderStyle.RoundedRect
          //textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
        }
      }
    }
  }

谢谢Yuyutsu。感谢Mahesh从一开始就提出最终解决方案,只是缺少了一些关键点。


问题答案:
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        for subView in searchBars.subviews  {
            for subsubView in subView.subviews  {
                if let textField = subsubView as? UITextField {
                     var bounds: CGRect
                bounds = textField.frame
                bounds.size.height = 35 //(set height whatever you want)
                    textField.bounds = bounds
                    textField.borderStyle = UITextBorderStyle.RoundedRect
//                    textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
                    textField.backgroundColor = UIColor.redColor()
//                    textField.font = UIFont.systemFontOfSize(20)
                }
            }
        }

    }

这可能对您有帮助:)



 类似资料:
  • 本文向大家介绍Objective C中可修改和不可以修改类型。相关面试题,主要包含被问及Objective C中可修改和不可以修改类型。时的应答技巧和注意事项,需要的朋友参考一下 答案:可修改不可修改的集合类。这个我个人简单理解就是可动态添加修改和不可动态添加修改一样。 比如NSArray和NSMutableArray。前者在初始化后的内存控件就是固定不可变的,后者可以添加等,可以动态申请新的内存

  • 本文向大家介绍componentWillUpdate可以直接修改state的值吗?相关面试题,主要包含被问及componentWillUpdate可以直接修改state的值吗?时的应答技巧和注意事项,需要的朋友参考一下 react组件在每次需要重新渲染时候都会调用, 例如,我们调用 时候 在这个函数中我们之所以不调用是因为该方法会触发另一个,如果我们中触发状态更改,我们将以无限循环结束.

  • 问题内容: 假设我想使用图像修饰指向某些文件类型的链接。我可以声明我的链接为 然后像CSS 现在,这很好用,除非不是我的链接文本的合适大小。 我希望能够告诉浏览器缩放图像,但是我终生无法找到正确的语法。还是像背景图片那样无法调整大小? ETA:我倾向于a)将源图像的大小调整为服务器端的“正确”大小,和/或b)更改标记以简单地内联提供IMG标签。我试图避免这两件事,但是它们听起来比单纯用CSS做事更

  • 新建MediaRecorder(Stream[,options]);

  • 我用如下所示的内容初始化Cytoscape:

  • 问题内容: 我有一个需要修改数据库表中列默认值的要求。该表已经是数据库中的现有表,并且当前该列的默认值为NULL。现在,如果将新的默认值添加到此列,如果我是正确的话,它将列的所有现有NULL更新为新的DEfault值。有没有一种方法可以不执行此操作,但仍在列上设置新的默认值。我的意思是我不想更新现有的NULL,并希望它们保留为NULL。 在这方面的任何帮助,不胜感激。谢谢 问题答案: 您对将会发生