質問

I can't get my head around this issue: I have a UISearchBar subclass that I'm using with a UISeachDisplayControlller in a UITableViewController that adds a button on the left side and makes the UISearchTextField smaller so it can fit both views.

I set the frames manually in layoutSubviews even tough I'm using AutoLayout across the project.

The code looks something like this:

UIView *searchBarView = [self.subviews objectAtIndex:0];
[searchBarView addSubview:_annotationsButton];


for (UIView *subview in searchBarView.subviews) {
    if ([subview isKindOfClass:[UITextField class]]) {
        // Change the border color of the UISearchTextField
        [subview.layer setBorderWidth:1.0];
        [subview.layer setBorderColor:[UIColor colorFromHexString:@"#77848D"].CGColor];
        [subview.layer setCornerRadius:2.0];
    }
}

[self setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];
self.separator = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1)];
[self.separator setBackgroundColor:[UIColor colorFromHexString:@"#d6d0cc"]];
[searchBarView addSubview:self.separator];

The strange result looks like this: enter image description here

As you can see, the bar is grayed out.

The layoutSubviews method is the following:

- (void)layoutSubviews{
[super layoutSubviews];

UIView *searchBarView = [self.subviews objectAtIndex:0];

for (UIView *subview in searchBarView.subviews) {
    if ([subview isKindOfClass:[UITextField class]]) {
        CGRect textFieldFrame = [subview frame];
        if (!subview.isFirstResponder) {
            self.originalFrame = textFieldFrame;
            CGRect newTextFieldRect = CGRectMake(self.originalFrame.origin.x + self.originalFrame.size.width /2,
                       self.originalFrame.origin.y,
                       self.originalFrame.size.width /2 - kPadding,
                       self.originalFrame.size.height);
            [subview setFrame:newTextFieldRect];

            CGRect annotationsButtonFrame = CGRectMake(kPadding,
                                                       self.originalFrame.origin.y,
                                                       self.originalFrame.size.width /2 - kPadding,
                                                       self.originalFrame.size.height);

            [self.annotationsButton setFrame:annotationsButtonFrame];
            [self.annotationsButton setHidden:NO];
        }
        else {
            [self.annotationsButton setHidden:YES];
        }
    }
}

[self.separator setFrame:CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1)];
}

In this method, I just adjust the frames of the UISearchBarTextField and _annotationsButton so they do not overlap.

役に立ちましたか?

解決

Whenever the background is set with an image this happens. I managed to set the bar translucent and with this line:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        self.searchBarStyle = UISearchBarStyleMinimal;
}

I fixed my issue.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top