UISearchBar height on iOS8 when showsScopeBar=YES

后端 未结 4 985
不思量自难忘°
不思量自难忘° 2020-12-29 10:53

I\'m using UISearchBar in my app, with showsScopeBar=YES. When running under iOS8 (both in the simulator and on a device) the scope bar is hidden a

相关标签:
4条回答
  • 2020-12-29 11:24

    Using Janne's answer, I thought it would be helpful to share how to do this automatically with method swizzling.

    @implementation UISearchBar (iOS8)
    
    static dispatch_once_t dispatchOnceToken;
    
    + (void)load {
        if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
        {
            dispatch_once(&dispatchOnceToken, ^{
                Class class = [self class];
    
                SEL originalViewWillAppearSelector = @selector(layoutSubviews);
                SEL swizzledViewWillAppearSelector = @selector(swizzled_layoutSubviews);
    
                Method originalViewWillAppearMethod = class_getInstanceMethod(class, originalViewWillAppearSelector);
                Method swizzledViewWillAppearMethod = class_getInstanceMethod(class, swizzledViewWillAppearSelector);
    
                if(class_addMethod(class, originalViewWillAppearSelector, method_getImplementation(swizzledViewWillAppearMethod), method_getTypeEncoding(swizzledViewWillAppearMethod)))
                {
                    class_replaceMethod(class, swizzledViewWillAppearSelector, method_getImplementation(originalViewWillAppearMethod), method_getTypeEncoding(originalViewWillAppearMethod));
                }
                else
                {
                    method_exchangeImplementations(originalViewWillAppearMethod, swizzledViewWillAppearMethod);
                }
            });
        }
    }
    
    - (void)swizzled_layoutSubviews
    {
        [self swizzled_layoutSubviews];
        [self sizeToFit];
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-29 11:25

    I ran into this problem too. After searching on Apples developer forum I found this thread: https://devforums.apple.com/thread/235803?start=0&tstart=0

    And apparently the SearchBar don't automatically does a sizeToFit when it's supposed too. So it's height stays at 44 instead of adjusting to the scope buttons. The bug is not fixed in the iOS8 GM.

    I did a simple [self.searchBar sizeToFit] in my viewWillAppear: and that solved it.

    0 讨论(0)
  • 2020-12-29 11:32

    This seems to be not an iOS 8 bug but a Xcode 6 GM compiled Storyboard bug as it happens on iOS 6/7 as well.

    As suggested it is fixed by calling sizeToFit on the search bar in viewWillAppear.

    If you want to do it from a view instead of a controller you can try to place it inside willMoveToWindow.


    This bug seems to affect Xcode 5.x builds on iOS 8 and Xcode 6 GM builds on all systems.

    0 讨论(0)
  • 2020-12-29 11:35

    This issue is occurring in the iOS 8 release version as well. I added these 2 lines in my viewWillAppear: and that solved it.

    - (void)adjustSearchBarToShowScopeBar{ [self.searchBar sizeToFit]; self.tableView.tableHeaderView = self.searchBar; } Just adding [self.searchBar sizeToFit] was covering up my tableview's first row. Just refreshing the tableview header fixed the issue perfectly.

    0 讨论(0)
提交回复
热议问题