gpt4 book ai didi

ios - UINavigationBar 中的 UITextField

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:16 24 4
gpt4 key购买 nike

我正在尝试使用以下代码将 UITextField 添加到 UINavigationBar 的右侧:

UITextField *txtField=[[UITextField alloc]init];
txtField.text=@"Text Goes Here";


txtField.translatesAutoresizingMaskIntoConstraints=NO;
[self.navigationController.navigationBar addSubview:txtField];



NSArray * constraints=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[txtField(100)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(txtField)];

[self.navigationController.navigationBar addConstraints:constraints];

碰巧程序崩溃说

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UINavigationBar's implementation of -layoutSubviews needs to call super.'

我不明白为什么...这样做的正确方法是什么?

最佳答案

尝试将您的文本字段添加为具有自定义 View 的 UIBarButtonItem,并将其设置为 navigationItem.rightBarButtonItem:

UITextField *txtField = [[UITextField alloc]init];
txtField.text = @"Text Goes Here";
txtField.translatesAutoresizingMaskIntoConstraints = NO;

UIBarButtonItem *rightBarButtonItem = [UIBarButtonItem alloc] initWithCustomView:txtField];

self.navigationController.navigationItem.rightBarButtonItem = rightBarButtonItem;

关于ios - UINavigationBar 中的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com