在导航栏下方添加“静态”搜索栏

时间:2017-05-17 12:01:08

标签: ios swift swift3 uinavigationbar uisearchbar

我做了很多搜索并阅读了很多问题/答案,但我仍然无法在导航栏下面编写静态搜索栏...(换句话说,一个搜索栏粘在导航栏上)< / p>

我想要那样的东西:

enter image description here

但我不知道该怎么做。

这是我写的代码:

let searchBar = UISearchBar()
      searchBar.placeholder = "Search"
      searchBar.frame = CGRect(x: 0, y: 0, width: (navigationController?.view.bounds.size.width)!, height: 64)
      searchBar.barStyle = .default
      searchBar.isTranslucent = false
      searchBar.barTintColor = UIColor.red
      searchBar.backgroundImage = UIImage()
      view.addSubview(searchBar)

当我向上滚动时,搜索栏不会粘在navigationBar上。我希望这两个部分有一个区块。

6 个答案:

答案 0 :(得分:0)

enter image description here

按照以下步骤在UISearchBar

下获得静态UINavigationBar
  1. 从Interface Builder中将UISearchBar拖到Storyboard中的ViewController上。

  2. 将约束设置为UISearchBar

    • 领导
    • 尾随
    • 固定高度

答案 1 :(得分:0)

在界面构建器(故事板中的视图控制器)中,拖动search bar并放在所需的位置。

然后设置top,leading,trailingfixed height等约束。

您将能够看到search bar坚持navigation bar

答案 2 :(得分:0)

我建议你查看你的AutoLayout&amp; Pin 4约束如下图所示 我希望它能解决你的问题。

enter image description here

答案 3 :(得分:0)

如果我的问题正确无误,您想要上下滚动屏幕,并在顶部看到您的搜索栏? 如果它是您想要的,您应该使用top,leading,trailing约束将searchBar固定到顶部。您应该将tableView或scrollView添加到viewcontroller。并将其固定在底部,领先,追溯到superview。并在tableView(或scrollView)和搜索栏之间添加垂直约束。

答案 4 :(得分:0)

只需在下方添加以下内容: view.addSubview(searchBar)

IF OBJECT_ID(N'dbo.IsWhiteSpace', N'FN') IS NOT NULL
    DROP FUNCTION dbo.IsWhiteSpace;
GO

-- Determines whether a single character is white-space or not (according to the UNICODE standard).
CREATE FUNCTION dbo.IsWhiteSpace(@c NCHAR(1)) RETURNS BIT
BEGIN
	IF (@c IS NULL) RETURN NULL;
	DECLARE @WHITESPACE NCHAR(31);
	SELECT @WHITESPACE = ' ' + NCHAR(13) + NCHAR(10) + NCHAR(9) + NCHAR(11) + NCHAR(12) + NCHAR(133) + NCHAR(160) + NCHAR(5760) + NCHAR(8192) + NCHAR(8193) + NCHAR(8194) + NCHAR(8195) + NCHAR(8196) + NCHAR(8197) + NCHAR(8198) + NCHAR(8199) + NCHAR(8200) + NCHAR(8201) + NCHAR(8202) + NCHAR(8232) + NCHAR(8233) + NCHAR(8239) + NCHAR(8287) + NCHAR(12288) + NCHAR(6158) + NCHAR(8203) + NCHAR(8204) + NCHAR(8205) + NCHAR(8288) + NCHAR(65279);
	IF (CHARINDEX(@c, @WHITESPACE) = 0) RETURN 0;
	RETURN 1;
END
GO

IF OBJECT_ID(N'dbo.Trim', N'FN') IS NOT NULL
    DROP FUNCTION dbo.Trim;
GO

-- Removes all leading and tailing white-space characters. NULL is converted to an empty string.
CREATE FUNCTION dbo.Trim(@TEXT NVARCHAR(MAX)) RETURNS NVARCHAR(MAX)
BEGIN
	-- Check tiny strings (NULL, 0 or 1 chars)
	IF @TEXT IS NULL RETURN N'';
	DECLARE @TEXTLENGTH INT = LEN(@TEXT);
	IF @TEXTLENGTH < 2 BEGIN
		IF (@TEXTLENGTH = 0) RETURN @TEXT;
		IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, 1, 1)) = 1) RETURN '';
		RETURN @TEXT;
	END
	-- Check whether we have to LTRIM/RTRIM
	DECLARE @SKIPSTART INT;
	SELECT @SKIPSTART = dbo.IsWhiteSpace(SUBSTRING(@TEXT, 1, 1));
	DECLARE @SKIPEND INT;
	SELECT @SKIPEND = dbo.IsWhiteSpace(SUBSTRING(@TEXT, @TEXTLENGTH, 1));
	DECLARE @INDEX INT;
	IF (@SKIPSTART = 1) BEGIN
		IF (@SKIPEND = 1) BEGIN
			-- FULLTRIM
			-- Determine start white-space length
			SELECT @INDEX = 2;
			WHILE (@INDEX < @TEXTLENGTH) BEGIN -- Hint: The last character is already checked
				-- Stop loop if no white-space
				IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, @INDEX, 1)) = 0) BREAK;
				-- Otherwise assign index as @SKIPSTART
				SELECT @SKIPSTART = @INDEX;
				-- Increase character index
				SELECT @INDEX = (@INDEX + 1);
			END
			-- Return '' if the whole string is white-space
			IF (@SKIPSTART = (@TEXTLENGTH - 1)) RETURN ''; 
			-- Determine end white-space length
			SELECT @INDEX = (@TEXTLENGTH - 1);
			WHILE (@INDEX > 1) BEGIN 
				-- Stop loop if no white-space
				IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, @INDEX, 1)) = 0) BREAK;
				-- Otherwise increase @SKIPEND
				SELECT @SKIPEND = (@SKIPEND + 1);
				-- Decrease character index
				SELECT @INDEX = (@INDEX - 1);
			END
			-- Return trimmed string
			RETURN SUBSTRING(@TEXT, @SKIPSTART + 1, @TEXTLENGTH - @SKIPSTART - @SKIPEND);
		END 
		-- LTRIM
		-- Determine start white-space length
		SELECT @INDEX = 2;
		WHILE (@INDEX < @TEXTLENGTH) BEGIN -- Hint: The last character is already checked
			-- Stop loop if no white-space
			IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, @INDEX, 1)) = 0) BREAK;
			-- Otherwise assign index as @SKIPSTART
			SELECT @SKIPSTART = @INDEX;
			-- Increase character index
			SELECT @INDEX = (@INDEX + 1);
		END
		-- Return trimmed string
		RETURN SUBSTRING(@TEXT, @SKIPSTART + 1, @TEXTLENGTH - @SKIPSTART);
	END ELSE BEGIN
		-- RTRIM
		IF (@SKIPEND = 1) BEGIN
			-- Determine end white-space length
			SELECT @INDEX = (@TEXTLENGTH - 1);
			WHILE (@INDEX > 1) BEGIN 
				-- Stop loop if no white-space
				IF (dbo.IsWhiteSpace(SUBSTRING(@TEXT, @INDEX, 1)) = 0) BREAK;
				-- Otherwise increase @SKIPEND
				SELECT @SKIPEND = (@SKIPEND + 1);
				-- Decrease character index
				SELECT @INDEX = (@INDEX - 1);
			END
			-- Return trimmed string
			RETURN SUBSTRING(@TEXT, 1, @TEXTLENGTH - @SKIPEND);
		END 
	END
	-- NO TRIM
	RETURN @TEXT;
END
GO

答案 5 :(得分:0)

如果你喜欢在搜索时添加到导航栏中的搜索栏并将代理和数据源提供给搜索栏,你应该将搜索控制器和添加搜索栏添加到该搜索控制器

self.searchbar.delegate = self
self.searchbar.datasource = self