如何在导航栏中更改按钮位置(How to change button position in navigation bar)

编程入门 行业动态 更新时间:2024-10-26 22:30:53
如何在导航栏中更改按钮位置(How to change button position in navigation bar)

所以我在导航栏中创建了一个按钮,并为其设置了一个图像。 这就是故事板中的样子: http : //imgur.com/cSRfHs3

所以我试图将它设置在屏幕边缘,这样图像和屏幕左侧之间就没有间隙了。

这是我用过的代码:

var screenWidth: CGFloat=0.0 var screenHeight: CGFloat=0.0 @IBOutlet weak var button1: override func viewDidLoad() { super.viewDidLoad() UIButton! var screenSize: CGRect = UIScreen.mainScreen().bounds screenWidth = screenSize.width screenHeight = screenSize.height button1.frame.size = CGSizeMake(133, 44) button1.frame.origin = CGPoint(x: screenWidth-(button1.frame.width+20), y: 20) self.view.addSubview(button1)

这是结果: http : //imgur.com/HZqxnqr

这是因为我将我的按钮直接添加到TableView吗?

So I've created a button in the navigation bar and set an image for it. This is how it looks like in storyboard: http://imgur.com/cSRfHs3

So I've tried to set it right on the edge of the screen so there will be no gap between the image and the left side of the screen.

Here is the code I've used for it:

var screenWidth: CGFloat=0.0 var screenHeight: CGFloat=0.0 @IBOutlet weak var button1: override func viewDidLoad() { super.viewDidLoad() UIButton! var screenSize: CGRect = UIScreen.mainScreen().bounds screenWidth = screenSize.width screenHeight = screenSize.height button1.frame.size = CGSizeMake(133, 44) button1.frame.origin = CGPoint(x: screenWidth-(button1.frame.width+20), y: 20) self.view.addSubview(button1)

And here is the result: http://imgur.com/HZqxnqr

Is this happening because I add my button straight to the TableView?

最满意答案

您需要将按钮直接放在导航栏中。 您还可以使用xcode storyboard gui为按钮添加约束。

You need to place the button directly in the navbar. Also you can add constraints to your button using the xcode storyboard gui.

更多推荐

本文发布于:2023-08-05 19:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:栏中   按钮   位置   如何在   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!