改变UIView位置的简单方法?

发布于 2024-10-19 19:52:17 字数 186 浏览 3 评论 0原文

我使用以下代码更改 UIView 的位置,而不更改视图的大小。

CGRect f = aView.frame;
f.origin.x = 100; // new x
f.origin.y = 200; // new y
aView.frame = f;

有没有更简单的方法来仅更改视图位置?

I change the position of a UIView with following codes without changing size of the view.

CGRect f = aView.frame;
f.origin.x = 100; // new x
f.origin.y = 200; // new y
aView.frame = f;

Is there more simple way to change only the view position?

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

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

发布评论

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

评论(13

故人的歌 2024-10-26 19:52:17
aView.center = CGPointMake(150, 150); // set center

aView.frame = CGRectMake( 100, 200, aView.frame.size.width, aView.frame.size.height ); // set new position exactly

aView.frame = CGRectOffset( aView.frame, 10, 10 ); // offset by an amount

编辑:

我还没有编译这个,但它应该可以工作:

#define CGRectSetPos( r, x, y ) CGRectMake( x, y, r.size.width, r.size.height )

aView.frame = CGRectSetPos( aView.frame, 100, 200 );
aView.center = CGPointMake(150, 150); // set center

or

aView.frame = CGRectMake( 100, 200, aView.frame.size.width, aView.frame.size.height ); // set new position exactly

or

aView.frame = CGRectOffset( aView.frame, 10, 10 ); // offset by an amount

Edit:

I didn't compile this yet, but it should work:

#define CGRectSetPos( r, x, y ) CGRectMake( x, y, r.size.width, r.size.height )

aView.frame = CGRectSetPos( aView.frame, 100, 200 );
昨迟人 2024-10-26 19:52:17

我也有同样的问题。我制作了一个简单的 UIView 类别来解决这个问题。

.h.m

#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

#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

I had the same problem. I made a simple UIView category that fixes that.

.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
卸妝后依然美 2024-10-26 19:52:17

UIView 也有一个 center 属性。如果您只想移动位置而不是调整大小,则可以更改它 - 例如:

aView.center = CGPointMake(50, 200);

否则,您将按照您发布的方式进行操作。

UIView's also have a center property. If you just want to move the position rather than resize, you can just change that - eg:

aView.center = CGPointMake(50, 200);

Otherwise you would do it the way you posted.

内心旳酸楚 2024-10-26 19:52:17

我发现了一种类似的方法(它也使用了一个类别),gcamp的答案对我帮助很大 这里。在您的情况下,就像这样简单:

aView.topLeft = CGPointMake(100, 200);

但是如果您想要水平居中并向左显示另一个视图,您可以简单地:

aView.topLeft = anotherView.middleLeft;

I found a similar approach (it uses a category as well) with gcamp's answer that helped me greatly here. In your case is as simple as this:

aView.topLeft = CGPointMake(100, 200);

but if you want for example to centre horizontal and to the left with another view you can simply:

aView.topLeft = anotherView.middleLeft;
音栖息无 2024-10-26 19:52:17

在使用自动布局的情况下,所选答案中的解决方案不起作用。如果您对视图使用自动布局,请查看此答案< /a>.

The solution in the selected answer does not work in case of using Autolayout. If you are using Autolayout for views take a look at this answer.

鯉魚旗 2024-10-26 19:52:17

CGRectOffset 此后已替换为实例方法 offsetBy

https://developer.apple.com/reference/coregraphics/cgrect/1454841- offsetby

例如,以前的内容

aView.frame = CGRectOffset(aView.frame, 10, 10)

现在是

aView.frame = aView.frame.offsetBy(dx: CGFloat(10), dy: CGFloat(10))

CGRectOffset has since been replaced with the instance method offsetBy.

https://developer.apple.com/reference/coregraphics/cgrect/1454841-offsetby

For example, what used to be

aView.frame = CGRectOffset(aView.frame, 10, 10)

would now be

aView.frame = aView.frame.offsetBy(dx: CGFloat(10), dy: CGFloat(10))
许久 2024-10-26 19:52:17
aView.frame = CGRectMake(100, 200, aView.frame.size.width, aView.frame.size.height);
aView.frame = CGRectMake(100, 200, aView.frame.size.width, aView.frame.size.height);
自此以后,行同陌路 2024-10-26 19:52:17

在我的工作中我们不使用宏。所以@TomSwift 提供的解决方案给了我启发。我看到了 CGRectMake 的实现并创建了相同的 CGRectSetPos 但没有宏。

CG_INLINE CGRect
CGRectSetPos(CGRect frame, CGFloat x, CGFloat y)
{
  CGRect rect;
  rect.origin.x = x; rect.origin.y = y;
  rect.size.width = frame.size.width; rect.size.height = frame.size.height;
  return rect;
}

使用时我只放置框架,X和Y

viewcontroller.view.frame = CGRectSetPos(viewcontroller.view.frame, 100, 100);

为我工作^_^

In my work we not use macros. So the solution provide by @TomSwift inspired to me. I see the implementation for CGRectMake and create the same CGRectSetPos but without macros.

CG_INLINE CGRect
CGRectSetPos(CGRect frame, CGFloat x, CGFloat y)
{
  CGRect rect;
  rect.origin.x = x; rect.origin.y = y;
  rect.size.width = frame.size.width; rect.size.height = frame.size.height;
  return rect;
}

To use I only put frame, X and Y

viewcontroller.view.frame = CGRectSetPos(viewcontroller.view.frame, 100, 100);

Work for me ^_^

牵强ㄟ 2024-10-26 19:52:17

如果有人需要轻型 Swift 扩展来轻松更改 UIView 边距 - 您可以使用 这个

view.top = 16
view.right = self.width
view.bottom = self.height
self.height = view.bottom

If anybody needs light Swift extension to change UIView margins easily - you can use this

view.top = 16
view.right = self.width
view.bottom = self.height
self.height = view.bottom
烛影斜 2024-10-26 19:52:17

@TomSwift Swift 3 答案

aView.center = CGPoint(x: 150, y: 150); // set center

aView.frame = CGRect(x: 100, y: 200, width: aView.frame.size.width, height: aView.frame.size.height ); // set new position exactly

aView.frame = aView.frame.offsetBy(dx: CGFloat(10), dy: CGFloat(10)) // offset by an amount

@TomSwift Swift 3 answer

aView.center = CGPoint(x: 150, y: 150); // set center

Or

aView.frame = CGRect(x: 100, y: 200, width: aView.frame.size.width, height: aView.frame.size.height ); // set new position exactly

Or

aView.frame = aView.frame.offsetBy(dx: CGFloat(10), dy: CGFloat(10)) // offset by an amount
悍妇囚夫 2024-10-26 19:52:17

迅速

view.frame = view.frame.offsetBy(dx: offsetX, dy: offsetY)

swift

view.frame = view.frame.offsetBy(dx: offsetX, dy: offsetY)
南汐寒笙箫 2024-10-26 19:52:17

这是 Swift 3 的答案,适合任何寻找的人,因为 Swift 3 不接受“Make”。

aView.center = CGPoint(x: 200, Y: 200)

Here is the Swift 3 answer for anyone looking since Swift 3 does not accept "Make".

aView.center = CGPoint(x: 200, Y: 200)
我们的影子 2024-10-26 19:52:17

其他方式:

CGPoint position = CGPointMake(100,30);
[self setFrame:(CGRect){
      .origin = position,
      .size = self.frame.size
}];

这我保存尺寸参数并仅更改原点。

Other way:

CGPoint position = CGPointMake(100,30);
[self setFrame:(CGRect){
      .origin = position,
      .size = self.frame.size
}];

This i save size parameters and change origin only.

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