[英]How to Put UIView subview In Places
For Example lets say I have a UIView I would like to display例如,假设我有一个想要显示的 UIView
view.addSubview(line)
Now I want t to be able to display that UIView in the top right corner.现在我希望 t 能够在右上角显示该 UIView。 Let say inside the right side of the navigation bar.
假设在导航栏的右侧。 How would I do this?
我该怎么做? Or even in a specific place.
甚至在特定的地方。
Add a frame for your subview, as :为您的子视图添加一个框架,如下所示:
Objective-C:目标-C:
line.frame = CGRectMake(0, 0, 50, 50);
Swift 3:斯威夫特 3:
CGRect(x:0,y:0,width: 50,height: 50)
This will add your line subview on top left corner with height 50 and width 50.这将在左上角添加您的行子视图,高度为 50,宽度为 50。
You can use您可以使用
self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: lineMat)
to set up a view on navigationBar right side.在导航栏右侧设置视图。
Also you can add the lineMat
view on UIApplication.shared.keyWindow
您也可以在
UIApplication.shared.keyWindow
上添加lineMat
视图
Just use UIApplication.shared.keyWindow?.addSubview(lineMat)
and set up to lineMat
view's frame.只需使用
UIApplication.shared.keyWindow?.addSubview(lineMat)
并设置为lineMat
视图的框架。
You will need to calculate the frame or need to give constraints to that view.您将需要计算框架或需要为该视图提供约束。
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
From above you can get screenwidth and you can set your y to 0. Let say width height of your view is 20. So, frame would be like below.从上面你可以得到 screenwidth 并且你可以将你的 y 设置为 0。假设你的视图的宽度高度是 20。所以,框架将如下所示。
yourView.frame = CGRect(x:screenwidth - 20 , y: 20, width:20 , height: 20)
You can change the frame as per your requirements.您可以根据需要更改框架。
For navigation bar there is a better way to do it by adding right bar item.对于导航栏,有一种更好的方法可以通过添加正确的栏项来实现。 Look below code how to do it.
看看下面的代码如何做到这一点。
self.navigationItem.rightBarButtonItem = rightButtonItem
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.