xon*_*rlz 121 iphone objective-c ipad ios
我有一个UIView内部UIViewm,我希望内部UIView总是在外部内部居中,而不必调整宽度和高度.
我设置了支柱和弹簧,使其位于顶部/左/右/底部而不设置调整大小.但它仍然没有中心.任何的想法?
Hej*_*azi 275
你可以这样做,它总是有效:
child.center = [parent convertPoint:parent.center fromView:parent.superview];
Run Code Online (Sandbox Code Playgroud)
而对于Swift:
child.center = parent.convert(parent.center, from:parent.superview)
Run Code Online (Sandbox Code Playgroud)
hap*_*pig 196
yourSubView.center = CGPointMake(yourView.frame.size.width / 2,
yourView.frame.size.height / 2);
Run Code Online (Sandbox Code Playgroud)
yourSubView.center = CGPoint(x: yourView.frame.size.width / 2,
y: yourView.frame.size.height / 2)
Run Code Online (Sandbox Code Playgroud)
Dan*_*rom 37
在我们开始之前,让我们提醒原点是CGPoint视图的左上角.关于观点和父母的重要事项.
让我们来看看这个简单的代码,一个视图控制器,它将视图添加到黑色方块:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createDummyView()
super.view.backgroundColor = UIColor.cyanColor();
}
func createDummyView(){
var subView = UIView(frame: CGRect(x: 15, y: 50, width: 50 , height: 50));
super.view.addSubview(subView);
view.backgroundColor = UIColor.blackColor()
}
}
Run Code Online (Sandbox Code Playgroud)
这将创建此视图:黑色矩形原点和中心的坐标与其父级相同
现在让我们尝试添加subView另一个SubSubView,并给subSubview提供与subView相同的原点,但是让subSubView成为subView的子视图
我们将添加以下代码:
var subSubView = UIView();
subSubView.frame.origin = subView.frame.origin;
subSubView.frame.size = CGSizeMake(20, 20);
subSubView.backgroundColor = UIColor.purpleColor()
subView.addSubview(subSubView)
Run Code Online (Sandbox Code Playgroud)
这就是结果:
因为这条线:
subSubView.frame.origin = subView.frame.origin;
Run Code Online (Sandbox Code Playgroud)
您希望紫色矩形的原点与它的父级(黑色矩形)相同,但它在它下面,为什么会这样?因为当你将视图添加到另一个视图时,subView框架"world"现在是它的父BOUND RECTANGLE,如果你有一个视图,它在主屏幕上的原点是coords(15,15)所有它的子视图,左上角将是(0,0)
这就是为什么你需要总是通过它的绑定矩形来引用父元素,这是它的子视图的"世界",让我们修改这一行:
subSubView.frame.origin = subView.bounds.origin;
Run Code Online (Sandbox Code Playgroud)
看到魔法,subSubview现在正好位于它的父源:
所以,你喜欢"好吧我只想通过父母的观点来看待我的观点,这有什么大不了的?" 好吧,这不是什么大不了的事,你只需要通过这样做"翻译"从它的框架中取出的父中心点到父母的边界中心:
subSubView.center = subView.convertPoint(subView.center, fromView: subSubView);
Run Code Online (Sandbox Code Playgroud)
你实际上是在告诉他"带父母查看中心,并将其转换为subSubView世界".
你会得到这个结果:
the*_*bot 26
我会用:
self.childView.center = CGPointMake(CGRectGetMidX(self.parentView.bounds),
CGRectGetMidY(self.parentView.bounds));
Run Code Online (Sandbox Code Playgroud)
我喜欢用这些CGRect选项......
SWIFT 3:
self.childView.center = CGPoint(x: self.parentView.bounds.midX,
y: self.parentView.bounds.midY);
Run Code Online (Sandbox Code Playgroud)
Cem*_*ker 19
首先禁用子视图自动调整
UIView *view1, *view2;
[childview setTranslatesAutoresizingMaskIntoConstraints:NO];
Run Code Online (Sandbox Code Playgroud)
如果你是UIView + Autolayout或Purelayout:
[view1 autoAlignAxis:ALAxisHorizontal toSameAxisOfView:view2];
[view1 autoAlignAxis:ALAxisVertical toSameAxisOfView:view2];
Run Code Online (Sandbox Code Playgroud)如果您只使用UIKit级自动布局方法:
[view1 addConstraints:({
@[ [NSLayoutConstraint
constraintWithItem:view1
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:view2
attribute:NSLayoutAttributeCenterX
multiplier:1.f constant:0.f],
[NSLayoutConstraint
constraintWithItem:view1
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:view2
attribute:NSLayoutAttributeCenterY
multiplier:1.f constant:0.f] ];
})];
Run Code Online (Sandbox Code Playgroud)我更喜欢:
UIView *parentView, *childView;
[childView setFrame:({
CGRect frame = childView.frame;
frame.origin.x = (parentView.frame.size.width - frame.size.width) / 2.0;
frame.origin.y = (parentView.frame.size.height - frame.size.height) / 2.0;
CGRectIntegral(frame);
})];
Run Code Online (Sandbox Code Playgroud)
使用IOS9,您可以使用布局锚点API.
代码如下所示:
childview.centerXAnchor.constraintEqualToAnchor(parentView.centerXAnchor).active = true
childview.centerYAnchor.constraintEqualToAnchor(parentView.centerYAnchor).active = true
Run Code Online (Sandbox Code Playgroud)
这种方法的优点是,CGPointMake或者CGRect使用这些方法,您将视图的中心设置为常量,但使用此技术,您将在两个视图之间设置永久保持的关系,无论parentview更改如何.
在你这样做之前一定要确定:
self.view.addSubview(parentView)
self.view.addSubView(chidview)
Run Code Online (Sandbox Code Playgroud)
并将translatesAutoresizingMaskIntoConstraints每个视图设置为false.
这样可以防止崩溃和AutoLayout干扰.
小智 7
您可以使用
yourView.center = CGPointMake(CGRectGetMidX(superview.bounds), CGRectGetMidY(superview.bounds))
Run Code Online (Sandbox Code Playgroud)
在Swift 3.0中
yourView.center = CGPoint(x: superview.bounds.midX, y: superview.bounds.midY)
Run Code Online (Sandbox Code Playgroud)
在视图和子视图中使用相同的中心是最简单的方法。你可以做这样的事情,
UIView *innerView = ....;
innerView.view.center = self.view.center;
[self.view addSubView:innerView];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
128680 次 |
| 最近记录: |