开发者

UINavigationBar - How to change the position of UIBarButtonItem?

开发者 https://www.devze.com 2023-04-10 16:52 出处:网络
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 b

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 bu开发者_StackOverflow中文版tton" 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
}


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];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号