簡體   English   中英

UIViewAutoresizingFlexibleLeftMargin,參考iOS上的Superview

[英]UIViewAutoresizingFlexibleLeftMargin with Reference to Superview on iOS

我正在動態地向表單中的字段添加錯誤圖標,如下所示:

錯誤圖標

我在代碼中生成了圖標,我想將它放在超級視圖的右上角 ,但我無法將它放在我想要的地方。 屏幕截圖顯示了它如何保持在左側,但它應該在右側。

這是我的代碼:

//Create the image
UIImageView *errorIcon =[[UIImageView alloc] initWithFrame:CGRectMake(-10,-10,23,23)];

//Set the image file
errorIcon.image = [UIImage imageNamed:@"error.png"];

//Tell the image to position it on the top-right (flexible left margin, flexible bottom margin)
errorIcon.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;

無論我將框架的x值設置為什么,我都無法將其固定在右側。

我在frame和/或autoResizingMask做錯了什么?

只有在調整視圖或超級視圖大小時才會啟用自動調整規則,而不是設置初始位置。 加上您的錯誤圖標原點(-10, -10)就在左上角的外面。 你可能需要的只是:

UIImageView *errorIcon = [[UIImageView alloc] initWithImage:errorImage];
errorIcon.center = CGPointMake(yourFieldView.frame.size.width, 0);
[yourFieldView addSubview:errorIcon];

你的問題是這一行:

UIImageView *errorIcon = 
    [[UIImageView alloc] initWithFrame:CGRectMake(-10,-10,23,23)];

這意味着超級視圖的左上角。 如果那不是你想放的地方,不要把它放在那里!

右上角是這里:

UIImageView *errorIcon = 
    [[UIImageView alloc] initWithFrame:
        CGRectMake(superview.bounds.size.width-10,-10,23,23)];

(對於superview替換對將成為superview的視圖的引用。)

暫無
暫無

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

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