UISearchBar文本域高度不增加

时间:2016-03-09 13:33:31

标签: ios objective-c uisearchbar

我正在执行以下操作来更改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;
}  

但它给了我这个:

enter image description here

如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

试试这个;

<强>的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);
            }
        }
    }
}

<强>夫特;

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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
                }
            }
        }
相关问题