簡體   English   中英

UIView在自定義xib中

[英]UIView in a custom xib

我在UIView的一個單獨的子類中創建了一個UIView,並使用了一個xib。

我在故事板中有一個帶有UIView的UIViewController,我想為這個UIView設置自定義UIView。

我將故事板UIView中的類名修改為自定義UIView的類名。 我在結果上獲得了UIView,但是使用了自定義UIView的幀大小。 我在自定義xib中添加了正確的約束。 由於錯誤的框架等,一些觸摸手勢無法正常工作

如果我做錯了,請建議我。

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self) {

        // Load the UIView from Interface Builder
        [[NSBundle mainBundle] loadNibNamed:@"TMPhotoEdit" owner:self options:nil];

        // Add UIView to current UIView object.
        [self addSubview:self.view];
    }

    return self;
}

self.view是我為它創建變量的相同視圖對象。

我希望框架設置與我在故事板中使用的框架相同。

使用代碼使其工作的解決方案也對我有很大幫助。 我的意思是,如果沒有故事板中的IBOutlet,只需添加這個UIView並設置一些框架。

如果我調用initWithFrame,我不能像上面的initWithCoder那樣設置xib,如果我在代碼中使用initWithFrame,則不會調用initWithCoder。

假設您有一個想要大小相似的視圖實例

self.view.frame = CGRectMake(otherView.frame.x, otherView.frame.y, otherView.frame.width, otherView.frame.height);

實際上你正在做的事情是不正確的,但無論如何你應該像這樣加載自定義視圖筆尖

self.view = [[NSBundle mainBundle] loadNibNamed:@"TMPhotoEdit" owner:self options:nil][0];

這又不正確,你為什么還需要呢? 但它應解決你的筆尖裝載問題。

暫無
暫無

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

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