Swift: Search Bar Created at Auto Focus

How to auto enable Search bar in UISearchController when that page appear?

Try to add to viewWillAppear self.searchController.searchBar.becomeFirstResponder()

Removing focus from Search Controller when view is loaded

Based on the image, your textfield is not first responder and you just don't want to see cancel button

you can set searchBar.setShowsCancelButton = false

or in storyboard, uncheck Shows Cancel Button

Sample Image

then if you want to show cancel button during editing, check the UISearchBarDelegate methods:

func searchBarTextDidBeginEditing

and set searchBar.setShowsCancelButton = true

func searchBarCancelButtonClicked

and set searchBar.setShowsCancelButton = false

UIScrollView with contentInset breaks UISearchBar autofocus

I met alomost the same problem and CodaFi's solution just cannot solve my problem.
So I kind of doubting this solution as the correct answer.

Here is the description of my situation and problem:
I want to add the UINavigationBar into the ContentInset area and UIWebView in ContentSize area of UIScrollView so that UINavigationBar can go and disappear with the web content outside the top of screen when scrolling down. And also inside the UINavigationBar I added one UITextField and one UISearchBar (yes, just like the Safari). Following the part of setting code(in viewDidLoad: method):

navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, -64, 320, 64);
textfield = [[UITextField alloc] initWithFrame:CGRectMake(5, 23, 150, 30)];
[navigationBar addSubview:textfield];
searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(200, 23, 100, 30)];
[navigationBar addSubview:searchbar];
[self.webView.scrollView setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];
[self.webView.scrollView setContentOffset:CGPointMake(0, -64) animated:NO];
[self.webView.scrollView addSubview:navigationBar];

The problem is that when I click the textfield or searchbar, the navigationbar(actually with the whole view) goes down in the screen: textfield by 41 points, and searchbar by 42 points(weird!). So it leaves a whole mysterious white space from 0 to 41(or 42) points on the upside of screen. And another weird thing is that it only happens once! After I scroll the screen by finger, it disappears and never shows up again the second time when I click the textfield or searchbar.
I believe the essence of my problem is just like Dennis's.

After searching on the web for two days, Unfortunately, I could not find the perfect solution to fix the essence of this problem(actually I am not even quite sure about the essence of the problem...).

But as I experimented and observed, my assumption would be that the 41 points that the textfield moved down could be because of the relative position it is set(-64 + 23 = -41) on the navigationbar. And when it is click by user, the system(iOS) checked its position(although I have already set the contentOffset by -64 in viewDidLoad:) and find it is not in the scope of the user and force the scrollview to scroll down until it shows up(41 points toward downside).
I can not understand the behavior of scrollview if we(developer) set a content inset area and interact with it. There is really few document and referrence talking about it. I wish my assumption was wrong and someone could correct me in the future and help us find the truth.

Last, although I could not find the "real" answer, actually I found a solution to this problem (like a remedy to cure the symptom). Here is the code:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y < -64) { // if it is beyond the contentInset area
if (!scrollView.dragging && !scrollView.decelerating) { // scrollView is operated by system
[scrollView setContentOffset:CGPointMake(0, -64) animated:NO]; // freeze the screen at (0, -64) position
}
}
}

How to add autofocus to AVCaptureSession? SWIFT

On my 6S the default camera focus mode is .ContinuousAutoFocus, which continuously focuses on whatever is taking up most of the camera's field of vision. Sounds like that's what you want.

You can check if your camera supports auto focus like so:

camera.isFocusModeSupported(.ContinuousAutoFocus)

and if it's not already set, set it like so:

try! camera.lockForConfiguration()
camera.focusMode = .ContinuousAutoFocus
camera.unlockForConfiguration()


Related Topics



Leave a reply



Submit