UINavigationBar - 如何更改 UIBarButtonItem 的位置?
我已经对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,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.
我正在设置自定义按钮的框架并将按钮放置在视图中
I'm setting the frame of a custom button and place the button in a view