简体   繁体   English

XCode Objective-C警告

[英]xcode objective-c warnings

I'm relatively new to objective-c...I'm using the iphone 3.0 SDK 我对Objective-C比较陌生...我正在使用iphone 3.0 SDK

I have a UIView, which is a subview, that I want to resize under certain circumstances. 我有一个UIView,它是一个子视图,我想在某些情况下调整大小。

The way I do it is within a controller class. 我的方法是在控制器类中。

for example, 例如,

CGSize el = CGSizeMake(30, 40);
[self.subview setSize:el];

the above code does work, but the compiler gives a warning: 'UIView' may not respond to 'setSize:' 上面的代码确实起作用,但是编译器发出警告:“ UIView”可能不响应“ setSize:”

At some level, "if it ain't broke, I don't want to fix it", but I'm a little worried that I'm doing something wrong. 在某种程度上,“如果它没有坏,我不想修复它”,但是我有点担心我做错了什么。

Any ideas as to why I'm getting the warning and how I can fix it? 关于为什么收到警告以及如何解决警告的任何想法?

TIA TIA

That probably means that setSize for UIView is implmented but it is not shown in the header file that is imported into the project. 这可能意味着将实现UIView setSize ,但不会在导入到项目中的头文件中显示它。 That makes it an undocumented API, ie one day it could change and break your code :) 这使它成为未公开的API,即有一天它可能会更改并破坏您的代码:)

And sure enough if you go to the documentation of UIView you will find no refrence to the size property. 并且很确定,如果您去UIView的文档,您不会发现对size属性的任何依赖。 So I would avoid it. 所以我会避免它。

What you should use instead is the frame property 您应该使用的是frame属性

CGSize el = CGSizeMake(30, 40);
CGRect bounds = CGself.subview.bounds;
bounds.size = el;
CGself.subview.bounds = bounds;

Give that a shot. 试一试。

The right thing to do here is use something else instead of the non-public size property. 正确的做法是使用其他方法代替非公共size属性。 But for the sake of discussion: If you wanted to get rid of the warning, you can declare that you know about the size property somewhere at the top of your implementation file: 但是为了讨论起见:如果您想摆脱警告,则可以声明您知道实现文件顶部的size属性:

#import "MyClass.h"

@interface UIView (private)
- (void) setSize: (CGSize) newSize;
@end

@implementation MyClass
…
@end

The compiler will stop complaining. 编译器将停止抱怨。

Here is a closer explanation using the "frame" property for "myView": 这是对“ myView”使用“ frame”属性的详细说明:

[self.myView.frame = CGRectMake(x, y, width, height)];

Where: 哪里:

  • x, coordinate FOR the top left corner of your view having as reference the top left corner of its parents "x" coordinate. x,视图的左上角坐标,以其父级“ x”坐标的左上角为参考。
  • y, same as x but y axis y,与x相同,但y轴
  • width, horizontal size of the frame 框架的宽度,水平尺寸
  • height, vertical size of the frame 高度,框架的垂直尺寸

iE You have a view which fits to the screen bounds, so its coordinate (0,0) will be the same as your device screen top left corner. IE您拥有一个适合屏幕范围的视图,因此其坐标(0,0)将与设备屏幕的左上角相同。 if you want to add a subview barely smaller that the screen size and center it horizontally and vertically, here is the set up: 如果您要添加一个几乎小于屏幕尺寸的子视图并将其水平和垂直居中,则设置如下:

[self.myView.frame = CGRMake ( 20 , 20 , self.view.frame.size.width-40, self.view.frame.size.height-40);

This example sets the frame inside the view and centered. 本示例将框架设置在视图内部并居中。 Note that we subtract 40 to the width corresponding to: 20 left side, 20 right side, and so the same for vertical adjustments. 请注意,我们减去40对应的宽度:左侧20,右侧20,对于垂直调整也是如此。

This code will also work in portrait and landscape mode. 该代码也可以在纵向和横向模式下工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM