隐藏 UINavigationItem 按钮时 UISegmentedControl 的对齐方式会发生变化

发布于 2024-12-03 06:51:08 字数 1088 浏览 1 评论 0原文

我在导航栏的标题视图中有一个 UISegmentedControl 。我还有右栏按钮(plus)和左栏按钮(编辑),如下图所示。

选择部门按钮

单击 ID 段时,我隐藏了两个栏按钮(此处为编辑和加号按钮)。这就像..

ID button selected

我正在使用以下代码来隐藏栏按钮项目。

- (void)segmentClicked {
    segment = segmentedControl.selectedSegmentIndex;
    self.navigationItem.leftBarButtonItem = leftBarButton;  //leftBarButton is an instance of UIBarButtonItem
    self.navigationItem.leftBarButtonItem = rightBarButton;  //rightBarButton is an instance of UIBarButtonItem

    if (segment == 0) {
        //Do something for segment 1 click
    }
    else if (segment == 1) {
        //Do something for segment 2 click
    }
    else {
        self.navigationItem.leftBarButtonItem = nil;
        self.navigationItem.rightBarButtonItem = nil;
    }
}

我的问题是,当我从segment1(部门)或segment2(名称)切换到segment3(ID)时,UISegmentedControl< /code> 对齐方式发生变化。我怎样才能让它稳定?

提前致谢

I have an UISegmentedControl in the title view of the navigation bar. Also i have right bar button(plus) and left bar button(Edit) as shown in the below image..

Department button selected

I'm hiding the two bar buttons (here, edit & plus buttons) when the ID segment is clicked. This will be like..

ID button selected

I'm using the following code to hide bar button items.

- (void)segmentClicked {
    segment = segmentedControl.selectedSegmentIndex;
    self.navigationItem.leftBarButtonItem = leftBarButton;  //leftBarButton is an instance of UIBarButtonItem
    self.navigationItem.leftBarButtonItem = rightBarButton;  //rightBarButton is an instance of UIBarButtonItem

    if (segment == 0) {
        //Do something for segment 1 click
    }
    else if (segment == 1) {
        //Do something for segment 2 click
    }
    else {
        self.navigationItem.leftBarButtonItem = nil;
        self.navigationItem.rightBarButtonItem = nil;
    }
}

My problem is, when i switch from segment1(Department) or segment2(Name) to segment3(ID), the UISegmentedControl alignment get changed. How can i make it stable?

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2024-12-10 06:51:08

栏按钮项目自动对齐..
如果删除其他项目,分段控件将会移动。
您需要做的是使用“固定空间”栏项目来定位分段控件......
不要将 leftBarButtonItem 设置为 nil,而是将其更改为保持分段控件就位所需大小的固定空间项。
如果你想在实现之前看看它是如何工作的,只需在 xib 中放置一个工具栏并添加“固定空间”和“灵活空间”项目。

编辑:

它非常简单。只需查看 uibarbuttonitem 类即可。创建一个固定空间类型的系统栏按钮项目对象,并根据您的需要设置其宽度。并将其设置为左栏按钮:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
item.width = 50;
self.navigationItem.leftBarButtonItem = item;
[item release];

编辑 2:

另一个建议 - 对片段使用较短的文本。例如使用 Dept. 而不是 Department。如果标题视图中的分段控件足够短,它将不会受到栏按钮的影响。

bar button items align automatically..
if you remove other items the segmented control will shift.
What you need to do is use a 'fixed space' bar item to position your segmented control...
instead of making your leftBarButtonItem nil, change it to a fixed space item of size required to keep the segmented control in place..
if you want to see how it works before implementing, just put a tool bar in a xib and add 'fixed space' and 'flexible space' items..

EDIT:

its pretty straight forward. Just look at the uibarbuttonitem class. create a system bar button item object of type fixed space and set it's width as per your need. and set it as your left bar button:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
item.width = 50;
self.navigationItem.leftBarButtonItem = item;
[item release];

EDIT 2:

another suggestion - use shorter text for the segments. like use Dept. instead of Department. if your segmented control in title view is short enough it wont be affected by bar buttons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文