该文本字段的UISearchBar的高度可以修改? [英] Can the height of the UISearchbar TextField be modified?

查看:445
本文介绍了该文本字段的UISearchBar的高度可以修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了我的用户界面,我觉得搜索栏有点太狭隘了我的胃口。我也希望确保人们视力较差或较差的手工技巧都没有问题,造就了他们想要的界面。

所以,我希望做的是调整的高度的UITextField 的UISearchBar

我曾尝试:
1.在故事板,添加的UISearchBar 高度约束 - 结果:搜索栏尺寸增大,但的UITextField 内保持不变

<醇开始=2>
  • 访问的UITextField 的UISearchBar 并修改它的高度 - 结果:控制台输出显示参数被修改,但在屏幕上时,的UITextField 的高度保持不变。

  • 请注意 - 我可以修改的其他参数的UITextField 使用方法2和的变化反映在屏幕上,所以我知道我得到的的UITextField

    code的方法2,放的ViewController的viewDidLoad中(),其中的UISearchBar位于:

     为tempView在(self.roomInfoSearchBar.subviews [0]作为!的UIView).subviews的! [UIView的]
    {
      如果让TV = tempView作为?的UITextField
      {
        VAR currentFrameRect = tV.frame
        currentFrameRect.size.height = 80
        //tV.layer.backgroundColor = UIColor.blueColor()。CGColor //这工作正常
        //tV.layer.cornerRadius = 5 //这工作正常
        //tV.font = UIFont(名称:信使,尺寸:40)//这工作正常
        的println(tV.frame.height)//控制台输出:0.0
        tV.frame = currentFrameRect
        的println(tV.frame)//控制台输出:(0.0,0.0,0.0,80.0)
        的println(currentFrameRect)//控制台输出:(0.0,0.0,0.0,80.0) - 多余的,只是检查
        的println(tV.frame.height)//控制台输出:80.0 - 它说80,但屏幕上显示搜索栏绝对不是80。
      }
    }

    我认为这是与自动布局在某种程度上能够忽略的参数,无论它是集。我想继续,因为它做了很多关于我的工作使用自动布局,但我希望它会表现得像是在故事板确实在那里,当用户设置的设置将其自动布局使用这些参数和抱怨时,它可以 T作为而不仅仅是无视无反馈的设置。

    编辑:

    在回应马赫什。我不知道很多客观的C ++,但我尽我所能来转换你写的进入迅捷。这是我有:

    (里面我viewcontroller.swift)

      FUNC viewDIdLayoutSubviews()
    {
      super.viewDidLayoutSubviews()
      self.roomInfoSearchBar.layoutSubviews()  VAR topPadding:浮法= 5.0
      对于视图(self.roomInfoSearchBar.subviews [0]作为!的UIView).subviews的! [UIView的]
      {
        如果让tfView =视图?的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))
        }
      }
    }

    我已经把我的code获取到文本框,因为我有一些问题,你的code转换为迅速。对于CGRectMake - 迅速抱怨各种类型包括topPadding不是CGFloat的和最后一个变量(Y大小),它再次不喜欢使用float混合CGFloat的,所以我不得不改变过

    不幸的是,它不会出现工作。我在故事板改变了高度的UISearchBar到80,我只是得到了与占地约总高度的1/3文本字段一个很高的搜索栏。

    编辑#2:结合Yuyutsu和马赫什code的修正版本
    还不够完善,但更接近。 Yuyutsu的code工作,但在我的评论中提到,本场没有中心,调整大小也是可见的(从跳跃高度的身高B)时,第一次加载视图。一个额外的缺陷是,由于调整大小是在 viewDidAppear 进行一次取向的变化,场返回到固有大小

    基于Yuyutsu的My code:

     覆盖FUNC viewDidAppear(动画:BOOL)
      {
        super.viewDidAppear(动画)
        VAR roomInfoSearchBarFrame = roomInfoSearchBar.frame
        VAR newHeight:CGFloat的= 60
        在roomInfoSearchBar.subviews子视图
        {
          在subView.subviews subsubView
          {
            如果让文本框= subsubView作为?的UITextField
            {
              VAR currentTextFieldFrame = textField.frame
              变种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的code我想知道还有什么地方我可以把这个调整,我碰到<少时href=\"http://kevindew.me/post/18579273258/where-to-progmatically-lay-out-views-in-ios-5-and\">link通过另一个计算器职位。根据我阅读,我看到了马赫什的回答应该工作。这是我明白了为什么它不工作 - 我需要的覆盖 FUNC viewWillLayoutSubviews()

    当前code:保持大小方向变化。但规模仍然跳可见(它不应该是可见的)

     覆盖FUNC viewWillLayoutSubviews()
      {
        super.viewWillLayoutSubviews()
        VAR roomInfoSearchBarFrame = roomInfoSearchBar.frame
        VAR newHeight:CGFloat的= 60
        在roomInfoSearchBar.subviews子视图
        {
          在subView.subviews subsubView
          {
            如果让文本框= subsubView作为?的UITextField
            {
              VAR currentTextFieldFrame = textField.frame
              变种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)
            }
          }
        }
      }

    所以,现在,唯一剩下的就是尝试,并使它所以文本域先于被看到的视图设置的大小,所以没有规模的转变,是对用户可见。

    最后编辑:完全工作code
    随着Yuyutsu的额外帮助,code下面做我想要的一切 - 在设定的尺寸开始了,现场为中心,涉及的旋转细

     覆盖FUNC viewDidLayoutSubviews()
      {
        super.viewDidLayoutSubviews()
        self.roomInfoSearchBar.layoutIfNeeded()
        self.roomInfoSearchBar.layoutSubviews()    VAR roomInfoSearchBarFrame = roomInfoSearchBar.frame
        VAR newHeight:CGFloat的= 60 //所需的文本框的高度。
        在roomInfoSearchBar.subviews子视图
        {
          在subView.subviews subsubView
          {
            如果让文本框= subsubView作为?的UITextField
            {
              VAR currentTextFieldBounds = textField.bounds
              currentTextFieldBounds.size.height = newHeight
              textField.bounds = currentTextFieldBounds
              textField.borderStyle = UITextBorderStyle.RoundedRect
              //textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
            }
          }
        }
      }

    由于Yuyutsu。
    感谢马赫什为pretty多想出最终的解决方案从一开始,只是缺少一些关键位。


    解决方案

     覆盖FUNC viewDidAppear(动画:BOOL){
            super.viewDidAppear(动画)
            对于子视图中searchBars.subviews {
                对于subsubView在subView.subviews {
                    如果让文本框= subsubView作为? {的UITextField
                         VAR界:的CGRect
                    边界= textField.frame
                    bounds.size.height = 35 //(设置任何你想要的高度)
                        textField.bounds =界限
                        textField.borderStyle = UITextBorderStyle.RoundedRect
    // textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
                        textField.backgroundColor = UIColor.redColor()
    // textField.font = UIFont.systemFontOfSize(20)
                    }
                }
            }    }

    这也许可以帮助你:)

    I'm testing out my UI and I find the search bar a little bit too narrow for my liking. I also want to make sure people with poorer vision or poorer manual dexterity have no issues bringing up the interface they want.

    So, what I would like to do is Adjust the height of the UITextfield inside UISearchbar.

    What I have tried: 1. In Storyboard, add UISearchbar Height constraint - Results: the searchbar size increases, but the UITextField inside stays the same.

    1. Access the UITextField inside UISearchbar and modify its height - Results: The console output shows that the parameter is modified, but on screen, the UITextField height remains the same.

    Note - I can modify other parameters of the UITextField using method 2 and the change is reflected on screen so I know I am getting to the UITextField

    Code for Method 2, put in viewDidLoad() of ViewController where UISearchbar is located:

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

    I think it has something to do with autolayout somehow being able to disregard parameters regardless of where it is set. I would like to continue to use autolayout since it does a lot of work for me, but I wish it would behave like it does in Storyboard where when a user sets a setting it will use those parameters in its autolayout and complain when it can't as opposed to just ignoring the setting without feedback.

    Edit:

    In response to Mahesh. I'm don't know much objective C++ but I tried my best to convert what you wrote into swift. This is what I have:

    (Inside my 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))
        }
      }
    }
    

    I've kept my code for getting to the textfield since I had some issues converting your code to swift. For the CGRectMake - swift complained about various types including that topPadding wasn't a CGFloat and for the last variable (Y size), again it did not like mixing CGFloat with Float so I had to change that too.

    Unfortunately, it does not appear to work. I changed the UISearchbar height to 80 in storyboard and I just got a very tall searchbar with the textfield covering about 1/3 of the total height.

    Edit #2: Incorporating Yuyutsu and corrected version of Mahesh code. Still not perfect, but closer. Yuyutsu's code works but as mentioned in my comments, the field does not center, the resize is also visible (jump from height A to height B) when the view is first loaded. One additional deficiency is that because the resize is done at viewDidAppear once the orientation changes, the field returns to the intrinsic size.

    My code based on Yuyutsu's:

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

    Looking at Yuyutsu's code I wanted to know where else I could put this adjustment and I came across link via another stackoverflow post. Based on what I read, I saw that Mahesh's answer should work. This is where I realized why it wasn't working - I needed to override func viewWillLayoutSubviews().

    Current code: Keeps size with orientation change. But size jump still visible (it shouldn't be visible)

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

    So right now, the only thing left is to try and make it so the textfield is the set size prior to the view being visible so there is no size shift that is visible to the user.

    Final Edit: Fully working code With Yuyutsu's additional help, code below does everything I want - starts off at the set size, field is centered, deals with rotation fine.

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

    Thanks Yuyutsu. Thanks Mahesh for pretty much coming up with the final solution right from the start, just missing a few critical bits.

    解决方案

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

    This might helps you :)

    这篇关于该文本字段的UISearchBar的高度可以修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆