简体   繁体   English

UISearchBar文本字段的高度没有增加

[英]UISearchBar textfield height not increasing

I am doing the following to change the height of UISearchBar and the textField inside it : 我正在执行以下操作来更改UISearchBar的高度和其中的textField:

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [searchBarEmployee setNeedsLayout];
    [searchBarEmployee layoutIfNeeded];

    for(UIView *view in searchBarEmployee.subviews)
    {
        for(UIView *subview in view.subviews)
        {
            if([subview isKindOfClass:[UITextField class]])
            {
                UITextField *txt = (UITextField*)subview;
                [txt setNeedsLayout];
                [txt layoutIfNeeded];
                CGRect rect = txt.bounds;
                rect.size.height = 80;
                txt.bounds = rect;
                txt.borderStyle = UITextBorderStyleRoundedRect;
            }
        }
    }

    CGRect bound = searchBarEmployee.bounds;
    bound.size.height = 100;
    searchBarEmployee.bounds = bound;
}  

But it is giving me this : 但这给了我这个:

在此处输入图片说明

How to fix this ? 如何解决呢?

Try this; 尝试这个;

Obj-C; OBJ-C;

- (void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];
    [searchBarEmployee layoutSubviews];

    for(UIView *view in searchBarEmployee.subviews)
    {
        for(UITextField *textfield in view.subviews)
        {
            if ([textfield isKindOfClass:[UITextField class]]) {
                textfield.frame = CGRectMake(0.0f, 0.0f, textfield.frame.size.width, 80.0f);
            }
        }
    }
}

Swift; 迅速;

for subView in homeView.subviews where subView is UITextField {
    subView.frame = CGRect(x: 0, y: 0, width: subView.frame.size.width, height: 80)
}

尝试本教程,自定义搜索栏会花费一些额外的工作,但是如果您要重用它,则值得: https//www.appcoda.com/custom-search-bar-tutorial/

Swift 3.0 version of working answer: Swift 3.0版本的工作答案:

  for view in (searchBar!.subviews) {
            for possibleTextField in view.subviews {
                if possibleTextField is UITextField {
                    let textField = possibleTextField as! UITextField
                    textField.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100) // New height here
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM