IOS Study Notes (7) UIButton UIImageView UIScrollView UIWebView

Source: Internet
Author: User

Use UIButton to add @ property (nonatomic, strong) UIButton * myButton; @ synthesize myButton;-(void) buttonIsPressed :( UIButton *) to the user interface *) paramSender {NSLog (@ "Button is pressed. ");}-(void) buttonIsTapped :( UIButton *) paramSender {NSLog (@" Button is tapped. ");}-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; self. myButton = [UIButton buttonWithType: UIButtonTypeRoundRect]; self. MyButton. frame = CGRectMake (110.0f, 2000000f, 1000000f, 370000f); [self. myButton setTitle: @ "Press Me" forState: UIControlStateNormal]; [self. myButton setTitle: @ "I'm Pressed" forState: UIControlStateHighlighted]; [self. myButton addTarget: self action: @ selector (buttonIsPressed :) forControlEvents: UIControlEventTouchDown]; [self. myButton addTarget: self action: @ selector (buttonIsTapped :) forControlEvents: UIControl EventTouchUpInside]; [self. view addSubview: self. myButton];} typedef enum {encoding = 0, encoding, encoding, UIButtonTypeInfoLight, encoding, UIButtonTypeContactAdd,} UIButtonType; Add the image: UIImage * normalImage = [UIImage imageNamed: @ "NormalBlueButton.png"]; UIImage * highlightedImage = [UIImage imageNamed: @ "highlightedBlueButton.png"]; self. myButto N = [UIButton buttonWithType: UIButtonTypeCustom]; self. myButton. frame = CGRectMake (110.0f, 2000000f, 1000000f, 370000f); [self. myButton setBackgroundImage: normalImage forState: UIControlStateNormal]; [self. myButton setTitle: @ "Normal" forState: UIControlStateNormal]; [self. myButton setBackgroundImage: highlightedImage forState: UIControlStateHighlighted]; [self. myButton setTitle: @ "Pressed" forState: UIControlStateHig Hlighted]; Use UIImageView to display the image @ property (nonatomic, strong) UIImageView * myImageView; @ synthesize myImageView;-(void) viewDidLoad {[super viewDidLoad]; UIImage * macBookAir = [UIImage imageNamed: @ "MacBookAir.png"]; self. myImageView = [[UIImageView alloc] initWithImage: macBookAir]; self. myImageView. center = self. view. center; [self. view addSubview: self. myImageView];} the image I downloaded online is 980*519 pixels. Of course it is not suitable for iPhone screens. The effect is certainly not what you want. How to solve this problem is a key: improve this problem by setting the contentMode attribute of the view. Typedef enum {UIViewContentModeScaleToFill, // fill the Image view with UIViewContentModeScaleAspectFit in the Image view, // ensure that the image in the Image view has the correct aspect ratio, make sure that the image adapts to the border UIViewContentModeScaleAspectFill of the Image view, // ensure the aspect ratio, and fill the image with the entire view border. In order to make this value work properly, ensure that the attribute clipsToBounds is set to YES. Audience, UIViewControlModeCenter, UIViewControlModeTop, audience, UIViewControlModeLeft, UIViewControlModeRight, audience,} UIViewContentMode; Use UIScrollView to create sliding content @ property (nonatomic, strong) UIImageView * myImageView; @ property (nonatomic, strong) UIScrollView * myScrol LView; @ synthesize myImageView; @ synthesize myScrollView;-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; UIImage * imageToLoad = [UIImage imageNamed: "macBookAir.png"]; self. myImageView = [[UIImageView alloc] initWithImage: imageToLoad]; self. myScrollView = [[UIScrollView alloc] initWithFrame: self. view. bounds]; [self. myScrollView addSubview: self. myImageView]; s Elf. myScrollView. contentSize = self. myImageView. bounds. size; [self. view addSubview: self. myScrollView];} Note: image size smaller than slide view size does not work. UIScrollViewDelegate Protocol: scrollViewDidScroll: This method is called when the content in the sliding view slides. ScrollViewWillBeginDeceleratin: This method is called when the user slides the content in the view and the finger leaves the screen and the content is still sliding. ScrollViewDidEndDecelerating: This method is called when the content in the sliding view ends sliding. ScrollViewDidEndDragging: willDecelerating: This method is called when the user completes dragging. (No sliding inertia) # import <UIKit/UIKit. h> @ interface customization: UIViewController <UIScrollViewDelegate> @ property (nonatomic, strong) UIImageView * myImageView; @ property (nonatomic, strong) UIScrollView * myScrollView; @ end-(void) scrollViewDidScroll :( UIScrollView *) scrollView {/* Gets called when user scrolls or drags */self. myScrollView. alpha = 0.50f;}-(void) ScrollViewDidEndDecelerating :( UIScrollView *) scrollView {/* Gets called only after scrolling */self. myScrollView. alpha = 1.0f;}-(void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {/* Make sure the alpha is reset even if the user is dragging */self. myScrollView. alpha = 1.0f;}-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor white Color]; UIImage * imageToLoad = [UIImage imageNamed: @ "MacBookAir.png"]; self. myImageView = [[UIImageView alloc] initWithImage: imageToLoad]; self. myScrollView = [[UIScrollView alloc] initWithFrame: self. view. bounds]; [self. myScrollView addSubview: self. myImageView]; self. myScrollView. contentSize = self. myImageView. bounds. size; self. myScrollView. delegate = self; [self. view addSubview: self. myScrollView];} Slide indicator (displayed by PAGE) self. myScrollView. indicatorStyle = UIScrollViewIndicatorStyleWhile; self. myScrollView. pagingEnabled = YES; // disable pagination-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; UIImage * iPhone = [UIImage imageNamed: @ "iPhone.png"]; UIImage * iPad = [UIImage imageNamed: @ "iPad.png"]; UIImage * macBookAir = [UIImage imageNamed: @ "MacBookAir.png"]; CGRect scrollViewRec T = self. view. bounds; self. myScrollView = [[UIScrollView alloc] initWithFrame: scrollViewRect]; self. myScrollView. pagingEnabled = YES; self. myScrollView. contentSize = CGSizeMake (scrollViewRect. size. width * 3.0f, scrollViewRect. size. height); [self. view addSubview: self. myScrollView]; CGRect imageViewRect = self. view. bounds; UIImageView * iPhoneImageView = [self newImageViewWithImage: iPhone frame: imageViewR Ect]; [self. myScrollView addSubview: iPhoneImageView];/* Go to next page by moving the x position of the next image view */imageViewRect. origin. x + = imageViewRect. size. width; UIImageView * iPadImageView = [self newImageViewWithImage: iPad frame: imageViewRect]; [self. myScrollView addSubview: iPadImageView];/* Go to next page by moving the x position of the next image view */imageViewRect. origin. x + = I MageViewRect. size. width; UIImageView * macBookAirImageView = [self newImageViewWithImage: macBookAir frame: imageViewRect]; [self. myScrollView addSubview: macBookAirImageView];} Use UIWebView to load the Web page loadData: MIMEType: textEncodingName: baseURL: // load an NSData instance to the Web View loadHTMLString: baseURL: // This method loads an NSString instance to the webpage view. This instance must be a valid HTML or something that the webpage can provide. LoadRequest: // load an NSURLRequest instance. This method is useful when you want to load a remote URL in the webpage view of the application. @ Property (nonatomic, strong) UIWebView * myWebView; @ synthesize myWebView;-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; self. myWebView = [[UIWebView alloc] initWithFrame: self. view. bounds]; [self. view addSubview: self. myWebView]; NSString * htmlString = @ "hello world <strong> Cookbook </strong>"; [self. myWebView loadHTMLString: htmlString baseURL: nil];} load the Apple master Page:-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; self. myWebView = [[UIWebView alloc] initWithFrame: self. view. bounds]; [self. view addSubview: self. myWebView]; NSURL * url = [NSURL URLWithString: @ "http://www.apple.com"]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; [self. myWebView loadRequest: request];} When Safari loads content, an activity indicator will appear in the upper-left corner of the screen. We also make an activity indicator: use protocol: UIWebViewDelegate # import <UIKit/UIKit. h> @ interface WebViewController: UIViewController <UIWebViewDelegate> @ property (nonatomic, strong) UIWebView * myWebView; @ end/* webViewDidStartLoad: This method is called when the webpage view starts to load content. webViewDidFinishLoad: when the webpage view is loaded, webView: didFailLoadWithError is called. This method is called when loading is stopped due to a loading error. */-(Void) webViewDidStartLoad :( UIWebView *) webView {[UIApplication sharedApplication] setNetworkActivityIndicatorVieible: YES];}-(void) webViewDidFinishLoad {[UIApplication sharedApplication] Principal: NO];}-(void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) error {[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO];} www.2cto.com-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundCOlor = [UIColor whiteColor]; self. myWebView = [[UIWebView alloc] initWithFrame: self. view. bounds]; self. myWebView. delegate = self; self. myWebView. scalesPageToFit = YES; [self. view addSubview: myWebView]; NSURL * url = [NSURL URLWithString: @ "http://www.apple.com"]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; self. myWebView loadRequest: request];}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.