Prosty sposób na zmianę pozycji UIView?

Zmieniam pozycję UIView z następującymi kodami bez zmiany rozmiaru widoku.

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

Czy jest prostszy sposób na zmianę tylko pozycji widoku?

Author: CharlesB, 2011-03-02

12 answers

aView.center = CGPointMake(150, 150); // set center

Lub

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

Lub

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

Edit:

Jeszcze tego nie skompilowałem, ale powinno działać:

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

aView.frame = CGRectSetPos( aView.frame, 100, 200 );
 216
Author: TomSwift,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-01 22:33:26

Miałem ten sam problem. Zrobiłem prostą kategorię UIView, która to naprawia.

.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
 32
Author: gcamp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-01 22:42:36

UIView ma również center właściwość. Jeśli chcesz tylko przesunąć pozycję, a nie zmienić jej rozmiaru, możesz to zmienić - np:

aView.center = CGPointMake(50, 200);

Inaczej zrobiłbyś to tak, jak napisałeś.

 12
Author: lxt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-01 22:23:41

Znalazłem podobne podejście (używa również kategorii) z odpowiedzią gcamp, która bardzo mi pomogła tutaj. W Twoim przypadku jest tak proste:

aView.topLeft = CGPointMake(100, 200);

Ale jeśli chcesz na przykład wyśrodkować poziomo i w lewo z innym widokiem, możesz po prostu:

aView.topLeft = anotherView.middleLeft;
 6
Author: Meda,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-25 17:39:21

Rozwiązanie w wybranej odpowiedzi nie działa w przypadku korzystania z Autolayout. Jeśli używasz Autolayout do wyświetlania, spójrz na tę ODPOWIEDŹ .

 5
Author: Sahil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:55:06
aView.frame = CGRectMake(100, 200, aView.frame.size.width, aView.frame.size.height);
 4
Author: Mark Adams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-01 22:24:39

CGRectOffset została zastąpiona metodą instancji offsetBy.

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

Na przykład to, co kiedyś

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

Would now be

aView.frame = aView.offsetBy(dx: CGFloat(10), dy: CGFloat(10))
 3
Author: Roope,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-12 22:15:32

W mojej pracy nie używamy makr. Tak więc rozwiązanie dostarczone przez @ TomSwift zainspirowało mnie. Widzę implementację dla CGRectMake i tworzę te same CGRectSetPos ale bez makr.

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;
}

Do użycia dodaję tylko ramkę, X i Y

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

Pracuj dla mnie ^_^

 2
Author: gzfrancisco,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-31 10:26:09

Jeśli ktoś potrzebuje rozszerzenia light Swift, aby łatwo zmienić UIView marginesy - można użyć tego

view.top = 16
view.right = self.width
view.bottom = self.height
self.height = view.bottom
 2
Author: katleta3000,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-28 10:17:25

@TomSwift Swift 3 odpowiedz

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

Lub

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

Lub

aView.frame = aView.frame.offsetBy(dx: CGFloat(10), dy: CGFloat(10)) // offset by an amount
 1
Author: Ayman Ibrahim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-23 10:08:49

Oto odpowiedź Swift 3 dla każdego, kto szuka, ponieważ Swift 3 nie akceptuje "Make".

aView.center = CGPoint(x: 200, Y: 200)
 0
Author: Pearson Basham,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-09 06:16:25

W inny sposób:

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

To zapisuję parametry rozmiaru i zmieniam tylko pochodzenie.

 0
Author: Bimawa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-17 14:31:32