UIToolbar精确尺寸

发布于 2024-12-20 15:23:00 字数 336 浏览 1 评论 0原文

我正在尝试借助工具栏自定义我的导航栏。 我以编程方式实现了它,如下所示:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 100, 44.01)];

然后将其添加到我的导航栏中。问题是我在边框上有这种丑陋的效果:

navigationbar + Toolbar

我尝试更改 y 和高度值,没有结果。 您有什么想法可以避免这种情况吗?

预先感谢,亚萨

I'm trying to customize my NavigationBar with the help of a toolbar.
I've implemented it programmatically as follows:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 100, 44.01)];

and then I added it to my NavigationBar. The problem is that I have this ugly effect on the borders:

navigationbar + toolbar

I've tried to change the y and the height values, with no results.
Do you have any ideas to avoid this?

Thanks in advance, yassa

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

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

发布评论

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

评论(3

浮生未歇 2024-12-27 15:23:00

我不会这样做。
您可以通过向 navigationItem.rightBarButtonItem 添加带有 2 个按钮的视图来实现相同的效果。这很简单:

// view that will hold the buttons
UIView* container = [[UIView alloc] init];

// create 1 button and add it to the container
UIButton* button = [[UIButton alloc] init........];
[container addSubview:button];


//create 2 button and add it to the container
button = [[UIButton alloc] init.........];
[container addSubview:button];


// now create a Bar button item
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:container];

// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = barButtonItem;

I wouldn't do it this way.
You can achieve the same effect by adding a view with 2 buttons to the navigationItem.rightBarButtonItem. it is very simple:

// view that will hold the buttons
UIView* container = [[UIView alloc] init];

// create 1 button and add it to the container
UIButton* button = [[UIButton alloc] init........];
[container addSubview:button];


//create 2 button and add it to the container
button = [[UIButton alloc] init.........];
[container addSubview:button];


// now create a Bar button item
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:container];

// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = barButtonItem;
囍笑 2024-12-27 15:23:00

我部分同意之前的回答和评论。
您建议的解决方案适用于自定义按钮。但是如果我想实现标准的编辑按钮怎么办?
对标准按钮/图标的访问是通过 UIBarButtonItem 类,而不是 UIButton。并且您无法将 UIBarButtonItem 对象添加到 UIView。

经过在网上进行大量研究后,我找到了完全满足我的要求的解决方案。工具栏必须按以下方式创建:

UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 95.0f, 44.01f)];
tools.tintColor = self.navigationController.navigationBar.tintColor;
tools.barStyle = -1;

这就是结果:

my工具栏

希望它有所帮助!
亚萨

I partially agree with previous answers and comments.
The solution you suggested works fine for custom buttons. But what if I want to implement standard Edit button?
Access to the standard buttons/icons is through the UIBarButtonItem class, not UIButton. And you can't add UIBarButtonItem objects to a UIView.

After many research on the web, I've found the solution that completely cover my requirement. The toolbar must be created in the following way:

UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 95.0f, 44.01f)];
tools.tintColor = self.navigationController.navigationBar.tintColor;
tools.barStyle = -1;

And this is the result:

my toolbar

Hope it helps!
yassa

巴黎夜雨 2024-12-27 15:23:00

或者你也可以用这种方式来做。
只需像这样创建 UIToolbar 的新子类

   @interface MyToolbar : UIToolbar

    @end

    @implementation MyToolbar

    - (id)initWithFrame:(CGRect)frame {

        if ((self = [super initWithFrame:frame])) {

            self.opaque = NO;
            self.backgroundColor = [UIColor clearColor];
            self.clearsContextBeforeDrawing = YES;      
        }
        return self;
    }


    - (void)drawRect:(CGRect)rect {
        // do nothing
    }

    - (void)dealloc {
        [super dealloc];
    }

@end

并将其用作普通 UIToolbar 即可。我不知道为什么,但它确实有效。

Or you can do it in this way.
Just create new subclass of UIToolbar like this

   @interface MyToolbar : UIToolbar

    @end

    @implementation MyToolbar

    - (id)initWithFrame:(CGRect)frame {

        if ((self = [super initWithFrame:frame])) {

            self.opaque = NO;
            self.backgroundColor = [UIColor clearColor];
            self.clearsContextBeforeDrawing = YES;      
        }
        return self;
    }


    - (void)drawRect:(CGRect)rect {
        // do nothing
    }

    - (void)dealloc {
        [super dealloc];
    }

@end

And use it as normal UIToolbar. I don't know why but it just works.

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