In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "iOS development how to create frame to achieve window window view view", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "iOS development how to create frame to achieve window window view view" bar!
UIScreen class
To display content on the screen, you first need to create a window to host the content. To create a window, you need a frame, and the underlying structure that contains border information is CGRect.
Each object that can display itself on the screen has a border that defines its display area, but many high-level view classes automatically calculate this information. The other classes are set by an initWithFrame initialization method when the view is initialized.
Let's get to know another class: UIScreen.
The UIScreen class represents the screen, through which we can get something we want.
You can test it with the following code:
CGRect screenBounds = [[UIScreen mainScreen] bounds]; / / return Rect CGRect viewBounds = [[UIScreen mainScreen] applicationFrame] with status bar; / / Rect / / screenBounds and viewBounds without status bar are both NSLog (@ "screenBounds.origin.x:%f", screenBounds.origin.x) relative to the device screen; NSLog (@ "screenBounds.origin.y:%f", screenBounds.origin.y) NSLog (@ "screenBounds.size.width:%f", screenBounds.size.width); NSLog (@ "screenBounds.size.height:%f", screenBounds.size.height); NSLog (@ "viewBounds.origin.x:%f", viewBounds.origin.x); NSLog (@ "viewBounds.origin.y:%f", viewBounds.origin.y); NSLog (@ "viewBounds.size.width:%f", viewBounds.size.width) NSLog (@ "viewBounds.size.height:%f", viewBounds.size.height)
Output result:
2014-08-13 17 47 purl 02.647 BookInsect [20956 Rd 907] screenBounds.origin.x:0.000000
2014-08-13 17 47 02.649 BookInsect [20956 R 907] screenBounds.origin.y:0.000000
2014-08-13 17 47 02.651 BookInsect [20956 VR 907] screenBounds.size.width:320.000000
2014-08-13 17 47 02.652 BookInsect [20956 VR 907] screenBounds.size.height:480.000000
2014-08-13 17 47 02.653 BookInsect [20956 lo 907] viewBounds.origin.x:0.000000
2014-08-13 17 47 02.655 BookInsect [20956 VR 907] viewBounds.origin.y:20.000000
2014-08-13 17 47 02.657 BookInsect [20956 lo 907] viewBounds.size.width:320.000000
2014-08-13 17 47 02.668 BookInsect [20956 lo 907] viewBounds.size.height:460.000000
UIView
Let's take a look at the UIView class, which inherits from UIResponder. By looking at the name, we know that it is the canvas responsible for display, if we compare window to a picture frame. We just constantly remove, change or overlay the canvas on the frame, or overlay other canvases on the canvas, and the size is, of course, determined by the painter. With the canvas, we can do whatever we want on it. This class is in UIView.h.
UIView* myView = [[UIView alloc] initWithFrame:CGRectMake (0.0jie 0.0200.0400.0)]; / / A canvas is created here, defining the position and size relative to the parent window. UIWindow
UIWindow inherits from UIView, which may be a bit of a logical barrier. How do frames inherit from the canvas? Don't go too far. Isn't the shape of the frame the same as the canvas? Can it be used as a frame to take a canvas and strengthen it in some ways? This is why one view can be added directly to another view.
Take a look at the initialization process of the system (in application didFinishLauchingWithOptions):
Self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor grayColor]; / / set a background color for window [self.window makeKeyAndVisible]; / / Let the window display for actual combat practice:
1) create a new project and select Empty Application whose name is practice
2) in application didFinishLaunchingWithOptions, you will find that the system has already built a picture frame. We will now use the system to build a picture frame for us. Of course, you can also build your own picture frame, but this is not necessary. Forget to mention that an application can only have one picture frame.
-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; / / Override point for customization after application launch. CGRect bound = [[UIScreen mainScreen] bounds]; NSLog (@ "boundwith:%f boundheight:%f", bound.size.width,bound.size.height); NSLog (@ "boundx:%f boundy:%f", bound.origin.x,bound.origin.y); CGRect appBound = [[UIScreen mainScreen] applicationFrame]; NSLog (@ "appBoundwith:%f boundheight:%f", appBound.size.width,appBound.size.height) NSLog (@ "appBoundx:%f boundy:%f", appBound.origin.x,appBound.origin.y); / / paint the first piece of canvas blue with a size of 320 X 100 CGRect CGone = CGRectMake (0.0,0.0,320,100); / / draw a rectangle with the initialization position and size UIView * v_one = [UIView alloc] initWithFrame:CGone]; / / initialize view v_one.backgroundColor = [UIColor blueColor] / / paint blue [self.window addSubview:v_one]; / / add it directly to the frame / / notice its position CGRect CGtwo = CGRectMake (0.0,100,160,100); / / draw a rectangle, initialization position and size UIView * v_two = [[UIView alloc] initWithFrame:CGtwo]; / / initialize view v_two.backgroundColor = [UIColor redColor]; / / paint red [self.window addSubview:v_two] / / superimposed on the frame / / Note his position CGRect CGthree = CGRectMake (160,100,160,100); / / UIView * v_three = [[UIView alloc] initWithFrame:CGthree]; / / v_three.backgroundColor = [UIColor greenColor]; / / [self.window addSubview:v_three]; / the fourth block pays attention to its position CGRect CGfour = CGRectMake (0.0,260,320,200) / / UIView * v_four = [[UIView alloc] initWithFrame:CGfour]; / / v_four.backgroundColor = [UIColor orangeColor]; / / [self.window addSubview:v_four]; / / fifth block, calculate its position and see its effect. / / you can try to move this code to the first fast initialization to try, there will be an unexpected effect CGRect CGfive = CGRectMake (100150160200) UIView * v_five = [[UIView alloc] initWithFrame:CGfive]; v_five.backgroundColor = [UIColor yellowColor]; [self.window addSubview:v_five]; self.window.backgroundColor = [UIColor grayColor]; / / [self.window makeKeyAndVisible]; / Last remember release v_one = nil; v_two = nil; v_three = nil; v_four = nil; v_five = nil; return YES / / self.window.backgroundColor = [UIColor whiteColor]; / / [self.window makeKeyAndVisible]; / / return YES;} get the size of the working area of the screen
IOS can be executed on many Apple devices, but the work area size Application Frame provided by each device is not the same. Here is a simple way to help you quickly find out the size of the current work area. The code is as follows.
1, the first is the part of the status column Status Bar.
/ / get the location and size of the StatusBar [self.view addSubview:theToolbar]; CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame]; NSLog (@\ "% @\", NSStringFromCGRect (statusBarRect))
2. Then there is the size of the workable area. If your application contains a status bar, then the size of the workable area will be the remaining area of the entire screen minus the status bar.
/ / get the location and size of the work area CGRect workSpaceRect = [[UIScreen mainScreen] applicationFrame]; NSLog (@\ "% @\", NSStringFromCGRect (workSpaceRect))
3, and finally, the size of the whole picture
/ / get the position and size of the entire screen CGRect windowRect = [[UIScreen mainScreen] bounds]; NSLog (@\ "% @\", NSStringFromCGRect (windowRect))
The above code stores the obtained size range information in a variable of CGRect type, and then displays the variable as a string.
Some common methods of managing Subview in UIView
A UIView can contain many Subview (other UIView), and these Subview have a so-called hierarchical relationship with each other, which is similar to the concept of layers in drawing software. The following code shows several common methods for managing layers (Subview). The code is as follows.
The first is the most commonly used to add and remove Subview.
/ / remove Subview from the current UIView [Subview removeFromSuperview]; / / add a Subview [UIView addSubview:Subview] to UIView
Move the Subview forward or backward one layer in the UIView. Moving forward will cover the Subview of the later layer, while moving back will be covered by the Subview of the upper layer.
/ / move Subview forward one layer (switch position with its previous layer) [UIView bringSubviewToFront:Subview]; / / move Subview back one layer (switch position with its next layer) [UIView sendSubviewToBack:Subview]
In UIView, use the index Index to exchange the layer levels of two Subview with each other.
/ / swap two layers [UIView exchangeSubviewAtIndex:indexA withSubviewAtIndex:indexB]
Use the variable name of Subview to get its indexed value (Index) in UIView.
/ / get Index NSInteger index = [[UIView subviews] indexOfObject:Subview name]
Add NSInteger's Tag to Subview so that they can distinguish each other later.
/ / add notes [Subview setTag:NSInteger]
Finally, get all the Subview in the UIView. Calling this method will return a NSArray and list the Subview in the order from back to front. The following figure lists all the Subview in the Root in the sample picture.
/ / all the Subview [UIView subviews] under UIView have come to this. I believe you have a better understanding of "how to create frame to achieve window window view view in iOS development". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.