UINavigationBar - 如何更改 UIBarButtonItem 的位置?

发布于 2024-12-07 19:05:45 字数 1414 浏览 1 评论 0原文

我已经对 UINavigationBar 进行了子类化。 NavigationBar 有两个 UIBarButtonItem 类型的按钮。可以移动这些按钮吗?例如,我想将“左按钮”向右移动50px,将“右按钮”向左移动50px。

LayoutSubViews 是移动这些按钮的正确位置吗?

这是 NavigationBar 类(在 MT 中创建):

[MonoTouch.Foundation.Register("NavigationBar")]
public class NavigationBar : UINavigationBar
{
    public NavigationBar (IntPtr handle) : base (handle)
    {
        Initialize ();
    }

    [Export ("initWithCoder:")]
    public NavigationBar (NSCoder coder) : base (coder)
    {
        Initialize ();
    }

    public NavigationBar(RectangleF frame, string title) : base (frame)
    {
        InitializeWithStyleAndTitle(title);
    }   

    void Initialize() { }

    void InitializeWithStyleAndTitle(string title)
    {
        //- title
        UINavigationItem navItem = new UINavigationItem(title);

        //- left button
        UIBarButtonItem leftBarButtonItem = new UIBarButtonItem("back", UIBarButtonItemStyle.Plain, this, new Selector("backAction"));
        navItem.LeftBarButtonItem = leftBarButtonItem;

        //- right button
        UIBarButtonItem rightBarButtonItem = new UIBarButtonItem("update", UIBarButtonItemStyle.Plain, this, new Selector("updateAction"));
        navItem.RightBarButtonItem = rightBarButtonItem;    

        //- style 
        BarStyle = UIBarStyle.Black;

        PushNavigationItem(navItem, false);
    }

    //- selectors omitted
}

I've subclassed a UINavigationBar. NavigationBar has two buttons of type UIBarButtonItem. Is it possible to move these button? For example I would like to move "left button" 50px right and "right button" 50px left.

Is LayoutSubViews the right place to move these button?

This is the NavigationBar class (created in MT):

[MonoTouch.Foundation.Register("NavigationBar")]
public class NavigationBar : UINavigationBar
{
    public NavigationBar (IntPtr handle) : base (handle)
    {
        Initialize ();
    }

    [Export ("initWithCoder:")]
    public NavigationBar (NSCoder coder) : base (coder)
    {
        Initialize ();
    }

    public NavigationBar(RectangleF frame, string title) : base (frame)
    {
        InitializeWithStyleAndTitle(title);
    }   

    void Initialize() { }

    void InitializeWithStyleAndTitle(string title)
    {
        //- title
        UINavigationItem navItem = new UINavigationItem(title);

        //- left button
        UIBarButtonItem leftBarButtonItem = new UIBarButtonItem("back", UIBarButtonItemStyle.Plain, this, new Selector("backAction"));
        navItem.LeftBarButtonItem = leftBarButtonItem;

        //- right button
        UIBarButtonItem rightBarButtonItem = new UIBarButtonItem("update", UIBarButtonItemStyle.Plain, this, new Selector("updateAction"));
        navItem.RightBarButtonItem = rightBarButtonItem;    

        //- style 
        BarStyle = UIBarStyle.Black;

        PushNavigationItem(navItem, false);
    }

    //- selectors omitted
}

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

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

发布评论

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

评论(2

会傲 2024-12-14 19:05:46

不,UINavigation 有特定的行为,只有左右按钮。如果您需要定位按钮,则应该使用 UIToolbar。在它们之间添加垫片以调整按钮的位置。

No, UINavigation has a specific behavior, left and right buttons only. You should use UIToolbar instead if you need to position the buttons. Add spacers in between to adjust the positioning of the buttons.

芯好空 2024-12-14 19:05:45

我正在设置自定义按钮的框架并将按钮放置在视图中

CustomButton *menu = [[CustomButton alloc] initWithFrame:CGRectMake(7, 3, 35, 35)];
[menu setImage:[UIImage imageNamed:@"sliding_drawer_white_icons"] forState:UIControlStateNormal];
UIView * menuButtonView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 46.0f, 40.0f)];
[menuButtonView addSubview:menu];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButtonView];

I'm setting the frame of a custom button and place the button in a view

CustomButton *menu = [[CustomButton alloc] initWithFrame:CGRectMake(7, 3, 35, 35)];
[menu setImage:[UIImage imageNamed:@"sliding_drawer_white_icons"] forState:UIControlStateNormal];
UIView * menuButtonView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 46.0f, 40.0f)];
[menuButtonView addSubview:menu];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButtonView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文