【问题标题】:View with subviews in a uinavigationbar and uitoolbar在 uinavigationbar 和 uitoolbar 中查看子视图
【发布时间】:2023-03-29 17:36:01
【问题描述】:

我真的需要你的帮助(关于 SO 的第一篇文章——要温柔):

我有两个动态 UIButton,我希望它们位于 UIView 的中心,而 UIView 又应位于 UINavigationbar 和 UIToolbar 的中心。我无法 - 尽管有很多谷歌搜索 - 似乎找到了一个正确的方法来做到这一点。

这是我到目前为止所做的:

在viewDidLoad中,我将两个按钮添加为子视图,并将视图设置为UINavigationbar的titleView

self.myClass.viewForTitleAndButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 32)];
[self.myClass.viewForTitleAndButton setBackgroundColor:[UIColor blackColor]];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myButton];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myOtherButton];
self.navigationItem.titleView = self.myClass.viewForTitleAndButton;

在我按下某些按钮时触发的方法中,我根据点击的内容设置其中一个按钮的标题(和边界):

CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName : [UIFont italicSystemFontOfSize:17.0]}];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat newX = (screenSize.width - titleSize.width) / 2;
CGRect buttonFrame = self.myClass.myButton.frame;

//Removing the line below doesn't do any difference at the moment
self.myClass.myButton.bounds = CGRectMake(newX, buttonFrame.origin.y, titleSize.width+8, buttonFrame.size.height);

[self.myClass.myButton setTitle:title forState:UIControlStateNormal];
NSLog(@"Title: %@", title);
//title is a NSString that changes depending on what is clicked. I am 100% sure it changes as I can see it in the log every time the method is triggered.

问题是 myButton 的标题没有改变。当它被放置在不同的位置而不是作为子视图时,它之前使用相同的按钮工作。

Q1:我缺少什么来改变按钮的标题?

Q2:将按钮作为子视图添加到视图中,然后分别放置在导航栏和工具栏中,这是实现我想要的合理方式吗?

这真的让我很烦恼,非常感谢任何正确方向的指针。

【问题讨论】:

  • 界限在变吗?还是这也不起作用?
  • 附加信息:myButton 有一个插座。我已经尝试过弱参考和强参考。
  • @Merlevede:我在 myButton 中添加了紫色背景颜色,紫色区域的大小保持不变。
  • 您的 myButton 插座似乎没有链接到 Interface Builder 中的按钮
  • 一开始我也怀疑是这样,但我只是检查了三次,它确实是链接的。按钮的标题在它不是 viewForTitleAndButton 的一部分之前已更改。

标签: ios iphone objective-c uinavigationbar title


【解决方案1】:

我认为您不能添加一个已经是视图控制器出口的按钮。换一种方式思考,一个按钮(视图)只能有一个父视图。

为标题视图动态创建按钮。

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *iv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
    iv.backgroundColor = [UIColor greenColor];
    titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    titleButton.frame = CGRectMake(0, 0, 120, 44);
    [titleButton setTitle:@"test" forState:UIControlStateNormal];
    [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [iv addSubview:titleButton];
    self.navigationItem.titleView = iv;
}

//some event to change the button title
- (void)click:(UIButton *)button
{
    [titleButton setTitle:@"I changed" forState:UIControlStateNormal];
}

【讨论】:

  • 感谢丹云的回复。您对其他方法有什么建议吗?
  • 为标题视图动态创建按钮。我编辑了我的答案。
  • 由于我的声望点太少,我必须将其发布为评论。 Q1 回答:它没有将 myButton 添加为导致它不起作用的子视图。恶意代码是这样的:self.navigationItem.titleView = self.myClass.viewForTitleAndButton;然而,这使得 myButton 的功能符合预期: self.navigationItem.titleView = self.myClass;不过,第二季度仍然存在。
  • 你是什么意思'恶意代码是这样的:self.navigationItem.titleView = self.myClass.viewForTitleAndButton'
  • 导致 myButton 为空的设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多