Please tell me how to avoid writing the following code on Objective-c each time.
CGSize sc=[[UISscreen mainScreen] bounds].size;
When I get the screen size of the terminal and do the layout, I write it down every time, but where should I declare it?
Thank you for your cooperation.
ios xcode objective-c
For screen size-dependent codes, I believe that writing the auto-layout will reduce the number of actual pixels in the screen size, but I will answer on the premise that it is separate from the auto-layout.
How about creating a subclass of UIApplication
and defining a read-only property called screenSize
and running screenSize at init
?
The sample code is as follows
[Application Name].h
#import<UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@ interface [application name]:UIApplication
@property(readonly) CGSize screenSize;
@end
NS_ASSUME_NONNULL_END
Application Name .m
#import "[Application Name].h"
@implementation [Application Name]
@synthesize screenSize;
- (nonnull instance type) init
{
self=[super init];
if(self){
screenSize = [[UISscreen mainScreen] bounds].size;
}
return self;
}
@end
To take advantage of this, add an item to info.plist
, select principal class
, and write the Application Name in the right column.
You can also use the screenSize
source file you want to browse to
#import "[Application Name].h"
where screenSize
is required.
CGSize screenSize=([Application Name] *) [UIAApplication sharedApplication]).screenSize;
You can then view the screenSize
obtained when starting the application.
After all, it's troublesome, so I don't think it's that different every time I write it, but I hope it'll be helpful if the variable I want to use again (readwrite
) and constant (readonly
) increase.
© 2024 OneMinuteCode. All rights reserved.