簡體   English   中英

自定義UIView旋轉時調整大小

[英]Custom UIView resizing while rotating

我知道有幾個類似的問題,但我仍然找不到合適的解決方案。 我剛剛創建了一個帶有自定義視圖的測試項目:

@interface CustomView : UIView

//..

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor redColor]];

    }
    return self;
}

和視圖控制器:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CustomView *cv = [(CustomView *) [CustomView alloc] initWithFrame:self.view.frame];
    [self.view addSubview: cv];
}

在景觀模式中,我們可以看到:

在此輸入圖像描述

怎么解決? 我知道layoutSubviews ,但是1.旋轉時沒有調用它; 2.我如何在此處調整視圖大小?

嘗試設置autoresizingMask。

CustomView *cv = [(CustomView *) [CustomView alloc] initWithFrame:self.view.frame];
cv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview: cv];

您還可以查看autolayout

https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles/UnderstandingAutolayout.html

問候,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM