加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 可以修改UISearchbar TextField的高度吗?

发布时间:2020-12-14 05:52:49 所属栏目:百科 来源:网络整理
导读:我正在测试我的用户界面,我发现搜索栏有点太狭窄,因为我的喜好。我也想确保视力较差或手术灵活性较差的人没有问题提出他们想要的界面。 所以,我想做的是调整UISextbar内的UISextbar的高度。 我试过了 1.在Storyboard中,添加UISearchbar Height约束 – 结
我正在测试我的用户界面,我发现搜索栏有点太狭窄,因为我的喜好。我也想确保视力较差或手术灵活性较差的人没有问题提出他们想要的界面。

所以,我想做的是调整UISextbar内的UISextbar的高度。

我试过了
1.在Storyboard中,添加UISearchbar Height约束 – 结果:搜索栏大小增加,但是内部的UITextField保持不变。

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

注意 – 我可以使用方法2修改UITextField的其他参数,更改反映在屏幕上,因此我知道我正在访问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,80.0)
    println(currentFrameRect) //console output:(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.
  }
}

我认为这与autolayout有关,无论何处设置,都可以忽略参数。我想继续使用autolayout,因为它为我做了很多工作,但是我希望它的行为就像在Storyboard中一样,当用户设置一个设置时,它将使用它的自动布局中的这些参数,并且当它可以“而不是仅仅忽略没有反馈的设置。

编辑:

回应Mahesh。我不知道很多目标C,但我尽力将您所写的内容转换成swift。这是我有的:

(在我的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))
    }
  }
}

我已经保留我的代码进入文本域,因为我有一些问题转换你的代码迅速。对于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文章碰到了link。根据我读到的内容,我看到Mahesh的答案应该有效。这是我意识到为什么它不工作的地方 – 我需要覆盖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,newHeight)

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

所以现在,唯一剩下的就是尝试使它成为textfield是在视图可见之前的集合大小,所以没有大小的偏移是可见的用户。

最终编辑:完全工作的代码
随着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)
                }
            }
        }

    }

这可能会帮助你:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读