我使用以下代码更改了UIView的位置,但没有更改视图的大小。
CGRect f = aView.frame;
f.origin.x = 100; // new x
f.origin.y = 200; // new y
aView.frame = f;有没有更简单的方法来改变视图的位置?
发布于 2011-03-02 06:42:37
我也有同样的问题。我创建了一个简单的UIView类别来解决这个问题。
.h
#import <UIKit/UIKit.h>
@interface UIView (GCLibrary)
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@end.m
#import "UIView+GCLibrary.h"
@implementation UIView (GCLibrary)
- (CGFloat) height {
return self.frame.size.height;
}
- (CGFloat) width {
return self.frame.size.width;
}
- (CGFloat) x {
return self.frame.origin.x;
}
- (CGFloat) y {
return self.frame.origin.y;
}
- (CGFloat) centerY {
return self.center.y;
}
- (CGFloat) centerX {
return self.center.x;
}
- (void) setHeight:(CGFloat) newHeight {
CGRect frame = self.frame;
frame.size.height = newHeight;
self.frame = frame;
}
- (void) setWidth:(CGFloat) newWidth {
CGRect frame = self.frame;
frame.size.width = newWidth;
self.frame = frame;
}
- (void) setX:(CGFloat) newX {
CGRect frame = self.frame;
frame.origin.x = newX;
self.frame = frame;
}
- (void) setY:(CGFloat) newY {
CGRect frame = self.frame;
frame.origin.y = newY;
self.frame = frame;
}
@end发布于 2011-03-02 06:23:41
UIView也有一个center属性。如果你只想移动位置而不是调整大小,你可以改变它-例如:
aView.center = CGPointMake(50, 200);
否则你就会按照你发布的方式来做。
发布于 2013-01-26 01:39:22
我发现了一种与gcamp的答案类似的方法(它也使用了一个类别),这对我的here很有帮助。在你的例子中是这样简单的:
aView.topLeft = CGPointMake(100, 200);但是,如果您想要水平居中并向左放置另一个视图,您可以简单地:
aView.topLeft = anotherView.middleLeft;https://stackoverflow.com/questions/5161096
复制相似问题