Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 是否可以修改UISearchbar文本字段的高度?_Swift_Autolayout_Uisearchbar - Fatal编程技术网

Swift 是否可以修改UISearchbar文本字段的高度?

Swift 是否可以修改UISearchbar文本字段的高度?,swift,autolayout,uisearchbar,Swift,Autolayout,Uisearchbar,我正在测试我的UI,我发现搜索栏有点太窄,我不喜欢。我还想确保视力较差或手动灵活性较差的人在使用他们想要的界面时没有问题 因此,我想做的是调整UITextfield内部UISearchbar的高度 我所尝试的: 1.在情节提要中,添加UISearchbarHeight约束-结果:搜索栏大小增加,但内部的UITextField保持不变 访问UITextField内部UISearchbar并修改其高度-结果:控制台输出显示参数已修改,但在屏幕上,UITextField高度保持不变 注意-我可以使用方

我正在测试我的UI,我发现搜索栏有点太窄,我不喜欢。我还想确保视力较差或手动灵活性较差的人在使用他们想要的界面时没有问题

因此,我想做的是调整
UITextfield
内部
UISearchbar
的高度

我所尝试的: 1.在情节提要中,添加
UISearchbar
Height约束-结果:搜索栏大小增加,但内部的
UITextField
保持不变

  • 访问
    UITextField
    内部
    UISearchbar
    并修改其高度-结果:控制台输出显示参数已修改,但在屏幕上,
    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, 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.
      }
    }
    
    我认为这与自动布局有一定关系,不管参数设置在哪里,自动布局都可以忽略参数。我想继续使用autolayout,因为它为我做了很多工作,但我希望它能像在故事板中一样,当用户设置设置时,它会在其autolayout中使用这些参数,并在无法使用时抱怨,而不是在没有反馈的情况下忽略设置

    编辑:

    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.
      }
    }
    
    作为对马赫什的回应。我不太清楚客观的C++,但我尽我所能把你写的东西转换成SWIFT。这就是我所拥有的:

    (在我的viewcontroller.swift中)

    自从我在将您的代码转换为swift时遇到一些问题以来,我一直保留用于访问文本字段的代码。对于CGRectMake-swift,它抱怨各种类型,包括topPadding不是CGFloat,对于最后一个变量(Y size),它同样不喜欢将CGFloat与Float混合,所以我也不得不改变它

    不幸的是,它似乎不起作用。在故事板中,我将UISearchbar的高度改为80,我得到了一个非常高的搜索栏,其中文本字段大约占总高度的1/3

    编辑#2:合并Yuyutsu和修正版Mahesh代码。 仍然不完美,但更接近。Yutsu的代码可以工作,但正如我在评论中提到的,当第一次加载视图时,字段不居中,调整大小也是可见的(从高度A跳到高度B)。另一个缺陷是,因为一旦方向改变,调整大小是在
    viewdide
    处完成的,因此字段会返回到固有大小

    我基于yutsu的代码:

    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.
      }
    }
    
    看着Yuyutsu的代码,我想知道我还能把这个调整放在哪里,我通过另一个stackoverflow帖子看到了。根据我读到的,我认为马赫什的答案应该是可行的。这就是我意识到它不起作用的原因-我需要重写
    func viewWillLayoutSubviews()

    当前代码:根据方向变化保持大小。但尺寸跳跃仍然可见(不应该可见)

    所以现在,唯一剩下的事情就是尝试使文本字段成为视图可见之前的设置大小,这样就没有用户可见的大小变化

    最终编辑:完全工作的代码 在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
            }
          }
        }
      }
    
    谢谢Yutsu。
    感谢Mahesh从一开始就提出了最终的解决方案,只是缺少了一些关键点。

    您可以更改内部文本字段的高度,请尝试以下代码

    步骤1)设置约束以增加UISearchBar的高度,然后使搜索栏退出,并在视图控制器中写下此代码

    - (void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
    
        [self.searchBar layoutSubviews];
    
        float topPadding = 5.0;
        for(UIView *view in self.searchBar.subviews)
        {
            for(UITextField *textfield in view.subviews)
            {
                if ([textfield isKindOfClass:[UITextField class]]) {
                    textfield.frame = CGRectMake(textfield.frame.origin.x, topPadding, textfield.frame.size.width, (self.searchBar.frame.size.height - (topPadding * 2)));
                }
            }
        }
    }
    
    此外,如果要更改搜索栏中的任何其他内容,则可以找到准确的基本元素,如UILabelUIImageUIView位于UIExtField子视图中,并且可以更改图像以及其他属性。谢谢

    编辑:

    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.
      }
    }
    
    正如你在swift中所要求的那样,我是用swift写的。我想你是做错了什么。请试试这个

    func viewDIdLayoutSubviews()
    {
        super.viewDidLayoutSubviews()
        self.roomInfoSearchBar.layoutSubviews()
    
        var topPadding: Float = 5.0
        for view in self.roomInfoSearchBar.subviews as [UIView] 
        {
            for viewNew in view.subviews as [UIView] 
            {
                if viewNew.isKindOfClass(UITextField) 
                {
                    viewNew.frame = CGRectMake(viewNew.frame.origin.x, CGFloat(topPadding), viewNew.frame.size.width, self.roomInfoSearchBar.frame.size.height - CGFloat(topPadding * 2))
                }
            }
        }
    }
    
    您将0索引视图作为要循环的第一个和最后一个视图。我想这就是你翻译的代码不起作用的问题。再试一次,请回复

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

    这可能会帮助您:)

    我发现公认的答案有时是错误的
    我就是这样解决了为搜索栏设置自定义高度的问题的:我已经设置了一个具有所需大小的彩色图像,并将其添加到
    viewDidLoad
    中搜索栏的文本字段中:

    let image = getImageWithColor(UIColor.whiteColor(), size: CGSizeMake(600, 60))
    searchBar.setSearchFieldBackgroundImage(image, forState: .Normal)
    
    然后是取自

    如果您使用:

    func setSearchFieldBackgroundImage(backgroundImage: UIImage?, forState state: UIControlState)   
    
    UISearchBar的UITextfield的高度将与图像的高度相同

    您可以插入图像,使用它自定义UISearchBar的UITextfield高度和背景图像

    或者,您可以使用以下功能创建圆角UIImage:

    func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
        let rect = CGRectMake(0, 0, size.width, size.height)
        let path = UIBezierPath(roundedRect: rect, cornerRadius: 5.0)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        path.fill()
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    UIImage的宽度被忽略。但是要有一个角半径,它最好大于
    10.0

    代码如下所示:

    let image = self.getImageWithColor(UIColor.lightGrayColor(), size: CGSizeMake(20, 20))
    searchBar.setSearchFieldBackgroundImage(image, forState: .Normal)
    

    这是有关如何更改UISearchBar高度的正确答案:

      let image = getImageWithColor(color: UIColor.clear, size: CGSize(width: 1, height: 40))
      searchBar.setSearchFieldBackgroundImage(image, for: .normal)
    
    使用以下方法:

      func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
            let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            color.setFill()
            UIRectFill(rect)
            let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
            return image
        }
    

    4号测试。六月。2018年Swift 4.2/iOS 12.1的更新:

    let newHeight : CGFloat = 45
    for subview in searchBar.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 = .roundedRect
                textField.contentVerticalAlignment = .center
            }
        }
    }
    

    对于swift 5.0,它工作良好:

    extension UIImage {
        class func image(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
            return UIGraphicsImageRenderer(size: size).image { rendererContext in
                color.setFill()
                rendererContext.fill(CGRect(origin: .zero, size: size))
            }
        }
    }
    
    extension UISearchBar {
        func customize() {
            setSearchFieldBackgroundImage(UIImage.image(color: .clear, size: CGSize(width: 1, height: 44)), for: .normal)
    
            // other settings
            searchBarStyle = .minimal
            searchTextPositionAdjustment = UIOffset(horizontal: 0, vertical: 0)
            tintColor = .black
            backgroundColor = .whiteBackground
            searchTextField.leftViewMode = .never
            searchTextField.textColor = .black
            searchTextField.backgroundColor = UIColor.lightGrayBackground
            searchTextField.layer.borderColor = UIColor.gray.cgColor
            searchTextField.cornerRadius = 22
            searchTextField.borderWidth = 0.5
        }
    }
    
    只需使用:

    let searchBar = UISearchBar()
    searchBar.customize()
    
    以下是Swift 5的代码。*:

    扩展UISearchBar{ func updateHeight(高度:CGFloat,半径:CGFloat=8.0){ 让图像:UIImage?=UIImage.imageWithColor(颜色:UIColor.clear,大小:CGSize(宽度
    let searchBar = UISearchBar()
    searchBar.customize()
    
    searchBar?.setSearchTextField(height: 48)
    
    UIImage GetImageWithColor(UIColor color, CGSize size)
    {
        var rect = new CGRect(0, 0, size.Width, size.Height);
        var path = UIBezierPath.FromRoundedRect(rect, 5);
        UIGraphics.BeginImageContextWithOptions(size, false, 0);
        color.SetFill();
        path.Fill();
        var image = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        return image;
    }
    
    var image = GetImageWithColor(UIColor.LightGray, new CGSize(20, 20));
    searchBar.SetSearchFieldBackgroundImage(image, UIControlState.Normal);