directory of this document
- 1. Repeated calls 2 times Loadview and Viewdidload
- 2. Button cannot be clicked
- [email protected] and [email protected]
- 4. Full screen display Default.png when launching the app
This article is designed to record some common bugs in development and common bits of knowledge, and I will update the content over time
Back to top 1. Repeat calls 2 times Loadview and Viewdidload
It is best not to change the visibility of the status bar in the Uiviewcontroller Loadview method (such as the status bar from display to hidden, or from hidden to display), because it causes repeated calls to the 2 Loadview and Viewdidload methods
Suppose that the status bar would have been displayed:
The following is the error code:
1-(void) Loadview {2 NSLog (@ "Loadview"); 3 //Hide Status bar 4 [UIApplication Sharedapplication].statusbarhidden = YES; 5 6 //..... Create UIView 7 self.view = [[[UIView alloc] Initwithframe:[uiscreen mainscreen].bounds] autorelease]; 8 Self.view.backgroundColor = [Uicolor Graycolor]; 9 }10 -(void) viewdidload { [super viewdidload];13 NSLog (@ "Viewdidload");
Operating effect:
Printing information:
1 2013-02-26 00:51:36.152 weibo[2251:c07] Loadview 2 2013-02-26 00:51:36.153 weibo[2251:c07] LoadView 3 2013-02-26 00:51:36.153 weibo[2251:c07] viewdidload 4 2013-02-26 00:51:36.154 weibo[2251:c07] ViewDidLoad
Although the effect is right, the system calls 2 successive Loadview and Viewdidload methods, resulting in the creation of 2 uiview, resulting in unnecessary overhead.
Cause Analysis:
The status bar becomes hidden by the display, meaning that the available height of the screen becomes longer, and the height of the Uiviewcontroller UIView is re-adjusted, so the system calls the Loadview method to create the UIView, and then calls the Viewdidload method again after the creation is complete.
Back to top 2. Button cannot be clicked
If you add a button to the Uiimageview, you will find that by default this button is not clickable and you need to set Uiimageview userinteractionenabled to Yes:
imageview.userinteractionenabled = YES;
When set to Yes, the button inside the Uiimageview can be clicked.
Cause Analysis:
• When the user taps the screen, a touch event is generated that will be added to an event queue managed by UIApplication
UIApplication will take the first event out of the event queue for distribution for processing, typically sending an event to the application's main window (UIWindow)
• The main window calls Hittest:withevent: Method finds the most appropriate UIView in the view (UIView) hierarchy to handle touch events
(hittest:withevent: Actually is a method of UIView, UIWindow inherit from UIView, so the main window UIWindow also belongs to the view of one)
Hittest:withevent: Method The approximate processing process is this:
First call the current view's Pointinside:withevent: method to determine whether the touch point is within the current view:
? If Pointinside:withevent: Method returns no, indicating that the touch point is not in the current view, the current view of the Hittest:withevent: return nil
? If the Pointinside:withevent: method returns Yes, indicating that the touch point is within the current view, traverse all the child views of the current view (subviews) and call the hittest:withevent of the Child View: Method repeats the previous steps, The traversal order of a child view is from top to bottom, that is, traversing forward from the end of the subviews array, until there is a hittest:withevent of the child View: The method returns a non-empty object, or all the child views are traversed:
? If the hittest:withevent of the child view is returned for the first time: The method returns a non-empty object, the current view's Hittest:withevent: method returns this object, processing ends
? If all child views are hittest:withevent: The method returns nil, the current view's Hittest:withevent: method returns the current view itself (self)
• Finally, this touch event is given to the main window's hittest:withevent: Method returns the View object to handle
I've roughly drawn a schematic of the iOS Touch event distribution:
Hittest:withevent: Method ignores the following views:
1> Hidden (hidden=yes) view
2> prohibit user action (Userinteractionenabled=no) view
View of 3> alpha<0.01
4> if the area of a child view exceeds the area of the parent view (if the parent view's Clipstobounds property is no, and the child view content that is more than the parent view area is displayed), then normal touch operations outside the parent view area will not be recognized because the parent view's Pointinside : Withevent: The method returns no so that it does not continue to traverse the child view down. Of course, you can also rewrite the pointinside:withevent: method to handle this
In summary, if the parent view is Userinteractionenabled=no, the touch event does not continue to pass down to the child view, so the child view can never handle touch events. Uiimageview, by default, Userinteractionenabled is No.
Back to top [email protected] and [email protected]
Because iOS devices have different screen resolutions and are small, displaying the same picture on a device may cause the image to be stretched and distorted, seriously impacting the user experience.
In order to make pictures in different devices can be very good display, the same kind of pictures we generally prepare 3 versions, such as the iOS program at the start of the full-screen display of default.png pictures:
(Retina is the retina screen)
default.png (image size 320x480): Displayed on a non-Retina-3.5 inch screen (iphone3g\iphone3gs, screen resolution is 320x480)
[email protected] (image size 640x960): Displayed on the Retina-3.5 inch screen (iphone4\iphone4s, screen resolved to 640x960)
[email protected] (image size 640x1136): Displayed on Retina-4.0 inch screen (iPhone5, screen resolution 640X1136)
Back to top 4. Full screen display Default.png when launching the app
Most apps display a background image in full screen during startup, such as Sina Weibo showing this one:
To achieve this in iOS, no pressure, very simple, the need to display the full-screen image default.png, the iOS app will be launched by default to load and full-screen display default.png.
You can also name pictures with other names, and configure them in Info.plist:
Once configured, the app will load and display full screen when it starts lufy.png
By default, the app is not really "full screen" when the default.png is displayed, because the top status bar is not hidden, such as the following effect:
In most cases, we want to hide the status bar so that the default.png is really full screen.
When it comes to this, someone might immediately think of a way: Add the following code to the Appdelegate application:didfinishlaunchingwithoptions: Method:
I can only say that your thinking is right, but actually do not achieve the desired effect, you will find the display default.png when the status bar is still there, and so on default.png display, the status bar is hidden.
Let me explain why this method is not feasible, in fact the reason is very simple:
1> Default.png is loaded during app startup, not after the app is launched
2> appdelegate Application:didfinishlaunchingwithoptions: The method is called after the app has been started.
Here's a solution to add a configuration to the Info.plist:
The yes here indicates that the status bar is hidden when the app is initialized (launched).
Of course, the status bar is still hidden after the default.png is displayed. If you want to re-display the status bar, fill in the following code:
Development FAQ Highlights