作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试下移我的搜索栏以与 Collection View 中其父单元格的底部对齐。
if(searchBarCellId == widgets[indexPath.item]){
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: searchBarCellId, for: indexPath) as! SearchBarCell
cell.backgroundColor = UIColor.white
cell.searchBar.backgroundImage = UIColor.white.image()
cell.searchBar.tintColor = UIColor.gray
let textFieldInsideSearchBar = cell.searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.backgroundColor = UIColor.lightGray
return cell
}
有没有办法以编程方式将搜索栏与单元格的底部单元格对齐?自定义 xib 编辑器不允许我添加约束。
使用编辑器添加约束
编辑代码:
if(searchBarCellId == widgets[indexPath.item]){
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: searchBarCellId, for: indexPath) as! SearchBarCell
cell.backgroundColor = UIColor.white
let frameWidth = UIScreen.main.bounds.width
let frameHeight = UIScreen.main.bounds.width*0.2
cell.searchBar.frame=CGRect(x: 0, y: frameHeight*0.47, width: frameWidth*0.7, height: frameHeight*0.5)
cell.searchBar.backgroundImage = UIColor.white.image()
cell.searchBar.tintColor = UIColor.gray
let textFieldInsideSearchBar = cell.searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.backgroundColor = UIColor.lightGray
return cell
}
最佳答案
InterfaceBuilder 的 .xib 编辑器确实允许您指定约束。您需要在文件检查器(右侧 Pane 中的第一个选项卡)中打开“使用自动布局”。
或者,您可以通过编程方式设置约束。
已编辑以获取更多信息
如何向搜索栏添加底部约束:
只选择搜索栏。然后点击 .xib 窗口右下角的 Add New Constraints 按钮(它看起来像领带战斗机图标)。点击图中中心框下方的细红线,将值设置为适当的值(可能接近 0),然后通过下拉该值旁边的弹出窗口来验证您将其绑定(bind)到的 View 。然后点击添加 1 个约束按钮。这应该将搜索栏的底部绑定(bind)到其父 View 的底部。
所以关键是您只想为搜索框设置相对于其直接父级的约束,在您的情况下,这将是单元格的 View ,而不管 xib 是否知道它将用于单元格。
关于ios - 如何将填充添加到搜索栏 swift ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49042943/
我是一名优秀的程序员,十分优秀!