UIView의 위치를 변경하는 간단한 방법?
뷰의 크기를 변경하지 않고 다음 코드로 UIView의 위치를 변경합니다.
CGRect f = aView.frame;
f.origin.x = 100; // new x
f.origin.y = 200; // new y
aView.frame = f;
보기 위치 만 변경하는 더 간단한 방법이 있습니까?
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 );
나는 같은 문제가 있었다. 이를 수정하는 간단한 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
.미디엄
#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
UIView에도 center
속성이 있습니다. 크기를 조정하는 대신 위치를 이동하려면 변경하면됩니다. 예 :
aView.center = CGPointMake(50, 200);
그렇지 않으면 게시 한 방식대로 할 것입니다.
나는 여기에서 나를 크게 도왔던 gcamp의 답변과 유사한 접근 방식 (카테고리 사용)을 발견 했습니다 . 귀하의 경우는 다음과 같이 간단합니다.
aView.topLeft = CGPointMake(100, 200);
그러나 예를 들어 수평 중앙에 다른 뷰를 사용하여 왼쪽에 배치하려면 다음과 같이하면됩니다.
aView.topLeft = anotherView.middleLeft;
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.
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))
aView.frame = CGRectMake(100, 200, aView.frame.size.width, aView.frame.size.height);
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 ^_^
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
@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
Here is the Swift 3 answer for anyone looking since Swift 3 does not accept "Make".
aView.center = CGPoint(x: 200, Y: 200)
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.
참고URL : https://stackoverflow.com/questions/5161096/simple-way-to-change-the-position-of-uiview
'Development Tip' 카테고리의 다른 글
Postgres.app으로 Mavericks에 pg gem을 설치할 수 없습니다. (0) | 2020.10.30 |
---|---|
google-places-api를 사용하는 간단한 HTML 페이지에 대한 ApiNotActivatedMapError (0) | 2020.10.30 |
Ruby로 캐리지 리턴을 제거하려면 어떻게해야합니까? (0) | 2020.10.30 |
Mac Spark-shell 오류 초기화 SparkContext (0) | 2020.10.30 |
C / C ++에서 배열을 복사하는 기능이 있습니까? (0) | 2020.10.30 |