我正在尝试将我的应用程序转换为Swift语言.
我有这行代码:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:textDictionary
forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
如何将其转换为Swift?
在Apple的文档中,没有这样的方法.
我一直在尝试搜索结果几个小时,但我无法理解这一点.也许这是不可能的.我正在尝试更改UISearchBar的占位符文本和放大镜的色调颜色.如果重要的话,我只针对iOS 8.0+.这是我的代码和它现在的样子:
let searchBar = UISearchBar()
searchBar.placeholder = "Search"
searchBar.searchBarStyle = UISearchBarStyle.Minimal
searchBar.tintColor = UIColor.whiteColor()
Run Code Online (Sandbox Code Playgroud)

我希望搜索和放大镜是白色的,或者可能是深绿色.
我有一个UISearchBar包含在a中,UIView并UIView已被分配给UITableView's tableHeaderView.我试图设计样式UISearchBar如下:

走出大门,这就是它的样子:

关于如何设计风格UISearchBar,搜索领域等等,有很多SO问题和答案,其中一些特别关注背景.其中许多涉及遍历UISearchBar's subviews,查找UISearchBarBackground(或_UISearchBarSearchFieldBackgroundView)对象,并直接设置其背景颜色.我正在尝试找到支持的API来执行此操作,但是......
该setSearchFieldBackgroundImage:forState:方法似乎有很大的潜力,但这是我使用1px白色(或透明)图像时得到的UIControlStateNormal:

有关如何使其工作的任何想法?我很感激有人指着我回答这个问题的SO帖子,但我确实认为我已经把它们都读了.万分感谢.
我正在尝试更改UISearchBar输入字段的背景颜色.这是您输入要搜索的文本的圆角视图,默认颜色为白色.我想把它改成灰色
我试过了:
for (UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UITextField")]) {
UITextField *textField = (UITextField *)subView;
[textField setBackgroundColor:[UIColor grayColor]];
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用:(
我还尝试将图像视图插入TextField,但似乎圆角视图与TextField分开.那么,有什么线索吗?
我想改变外观UISearchBar:所以,
如果有一种方法可以使我 UITextField(在自定义搜索背景中)的功能如何UISearchBar?或继承和覆盖- (void)layoutSubviews是唯一的方法?
请告诉如何继承它!
我正在尝试为搜索栏中的文本字段设置背景颜色。起初我是这样做的:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].backgroundColor = [UIColor blueColor];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用。颜色保持白色。好的。我通过调用以下代码使其工作:
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
self.tableView.tableHeaderView = searchBar;
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].backgroundColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)
从viewDidLoad。但它只能工作一次。当打开此类的另一个视图控制器时,背景颜色保持不变。
你可以自己尝试一下。只需创建一个简单的 iOS 应用程序模板并添加UISearchBar. 然后尝试通过设置其文本字段的背景颜色UIAppearance。
怎么了?这是 iOS 的 bug 吗?我知道我可以遍历搜索栏的所有子视图,查找UITextField并设置其背景颜色,但我想知道发生了什么。
我按照下面的编程方式创建了UISearchBar,按钮看起来是第二个图像.如何清除搜索栏的白色并将背景颜色设置为黑色
var categorySearchBar : UISearchBar = UISearchBar()
categorySearchBar.frame = CGRectMake(0, 20, self.view.frame.size.width, 30)
categorySearchBar.layer.borderColor = UIColor.grayColor().CGColor
categorySearchBar.layer.borderWidth = 1.0
categorySearchBar.placeholder = "SEARCH"
categorySearchBar.barTintColor = UIColor.blackColor()
self.view.addSubview(categorySearchBar)
Run Code Online (Sandbox Code Playgroud)


ios ×8
uisearchbar ×6
iphone ×3
objective-c ×3
swift ×3
uiappearance ×2
uikit ×1
uitextfield ×1
xcode ×1