使用UIButton给用户界面添加按钮
@property(nonatomic,strong)UIButton *myButton;
@synthesize myButton;
-(void)buttonIsPressed:(UIButton *)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,200.0f,100.0f,37.0f);
[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:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
}
typedef enum{
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
}UIButtonType;
按钮上增加图片:
UIImage *normalImage = [UIImage imageNamed:@"NormalBlueButton.png"];
UIImage *highlightedImage = [UIImage imageNamed:@"highlightedBlueButton.png"];
self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.myButton.frame = CGRectMake(110.0f,200.0f,100.0f,37.0f);
[self.myButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[self.myButton setTitle:@"Normal" forState:UIControlStateNormal];
[self.myButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[self.myButton setTitle:@"Pressed" forState:UIControlStateHighlighted];
使用UIImageView显示图片
@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];
}
我在网上下载的图片是 980*519的像素,当然不适合iPhone的屏幕了,如果像上面那样加载,得到的效果肯定不是你想要的。怎么解决这个问题是个关键:
通过设置视图的contentMode属性来改善这个问题。
typedef enum{
UIViewContentModeScaleToFill,//图片视图里的图片将图片视图填满
UIViewContentModeScaleAspectFit,//确保图片视图里的图片有正确的长宽比,并且会确保图片适应图片视图的边界
UIViewContentModeScaleAspectFill ,//确保长宽比,并且使图片充满整个视图边界,为了能使这个值正常工作,确保将 clipsToBounds这个属性设为YES。
UIViewContentModeRedraw,
UIViewControlModeCenter,
UIViewControlModeTop,
UIViewControlModeButtom,
UIViewControlModeLeft,
UIViewControlModeRight,
UIViewControlModeTopLeft,
UIViewControlModeTopRight,
UIViewControlModeBottomLeft,
UIViewControlModeBottomRight,
}UIViewContentMode;
使用UIScrollView创建能滑动的内容
@property( nonatomic,strong)UIImageView *myImageView;
@property( nonatomic,strong)UIScrollView *myScrollView;
@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];
self.myScrollView.contentSize = self.myImageView.bounds.size;
[self.view addSubview:self.myScrollView];
}
注:图片尺寸小于滑动视图尺寸不起作用。
UIScrollViewDelegate协议:
scrollViewDidScroll:当滑动视图里的内容滑动时将调用这个方法。
scrollViewWillBeginDeceleratin:当用户滑动滑动视图里的内容并且手指离开屏幕而且内容还在滑动的时候,将会调用这个方法。
scrollViewDidEndDecelerating :当滑动视图里的内容结束滑动时将调用这个方法。
scrollViewDidEndDragging: willDecelerating: 当用户完成拖拽将会调用这个方法。(无滑动惯性)
#import <UIKit/UIKit.h>
@interface Creating_Scrollable_Content_with_UIScrollViewViewController : 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 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]; self.myScrollView.contentSize
= self.myImageView.bounds.size; self.myScrollView.delegate = self;
[self.view addSubview:self.myScrollView];
}
滑动指示器(可以分页显示内容)
self.myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhile;
self.myScrollView.pagingEnabled = YES;//禁用分页
- (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 scrollViewRect = 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:imageViewRect];
[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 += imageViewRect.size.width; UIImageView *macBookAirImageView =
[self newImageViewWithImage:macBookAir frame:imageViewRect];
[self.myScrollView addSubview:macBookAirImageView];
}
使用UIWebView加载Web页面
loadData: MIMEType: textEncodingName: baseURL: //加载一个NSData的实例到网页视图上
loadHTMLString: baseURL: //这个方法是加载NSString的一个实例到网页视图上,这个实例必须是一个有效的HTML,或者说是那些网页能提供的东西。
loadRequest://加载一个NSURLRequest的实例,当你想要在应用程序的网页视图里加载远程的URL时,这个方法是很有用的。
@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];
}
加载Apple的主页面:
-(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];
}
Safari加载内容时屏幕左上角会出现一个活动指示器,我们也做一个:
使用协议 :UIWebViewDelegate
#import <UIKit/UIKit.h>
@interface WebViewController:UIViewController<UIWebViewDelegate>
@property(nonatomic,strong)UIWebView *myWebView;
@end
/* webViewDidStartLoad:当网页视图开始加载内容时将调用这个方法
webViewDidFinishLoad: 当网页视图完成加载时将调用这个方法
webView:didFailLoadWithError: 当因加载出错而导致停止加载时将调用这个方法。
*/
-(void)webViewDidStartLoad:(UIWebView *)webView{
[ [UIApplication sharedApplication]setNetworkActivityIndicatorVieible:YES];
}
-(void)webViewDidFinishLoad{
[ [UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[ [UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
-(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];
}
分享到:
相关推荐
IOS UIScrollView + UIButton 实现页面和顶部标签页水平滚动效果,可以运用到实际项目中
iOS 自定义UITableViewCell上添加UIButton按钮点击实现,知道点击了是哪个cell上的按钮。用代理跟代码块两种方式实现。
ios 自定义uibutton
通过UIButton和UIScrollView自定义SegmentController
UIButton,UILabel文字旋转(倾斜),UIButton,UILabel文字旋转(倾斜)
2018-01-22 新增功能 * 新增自定义计数器功能 (仿淘宝, 京东, 美团, 饿了吗, 百度外卖 计数功能) 2018-01-17 新增功能 * 新增自定义View * 新增倒计时功能 2018-01-16 初始版本 * 自定义图片上下左右位置 ...
以上所述是小编给大家介绍的IOS倒计时设置UIButton标题title的抖动问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对软件开发网网站的支持! 您可能感兴趣的...
iOS学习笔记 十年生死两茫茫,不思量,自难忘,华年短暂,陈辞岁月悠悠伤, 满腔热血已芜荒,展未来,后生强,战战兢兢,如履薄冰心彷徨, 青丝化雪、鬓角成霜,已是英雄迟暮,人生怎慷慨激昂? 目录 内存管理(五) ...
UIImageView一个视图扩展了 ImageView,就像 iOS 中的 UIButton。截图用法步骤1 ####Gradle 和 Maven dependencies { compile ' me.drakeet.library:androiduiview:1.0.0 '}< dependency> < groupId>me.drakeet....
相信每位iOS开发者都知道在UIButton中可以设置图片和文字,也经常见到同时设置有图片和下方提示文字的按钮,但是当我自己去对图片按钮添加提示文字的时候,却发现这并不是想象中的那么简单。怎么不简单呢?下面来...
使用一句代码为UIbutton增加不同状态的配置(borderColor, titleLabelFont, backgroundColor,subView) 最新代码请移步github: https://github.com/spWang/UIButton-State 如果在使用过程中发现问题,请联系我,...
借鉴相关资料,整理了一个很有意思的button动画效果,iOS自定义UIButton点击动画特效 先看一下效果图: 下面贴上代码: ViewController: #import @interface ViewController : UIViewController @end #import ...
xamarin学习笔记B03(ios的UIView的了解)它是所有视图控件的父类。例如UIButton、UILabel、UIImageView等都最终是继承自UIView。它可以添加其它视图作为它的子视图。
UIButton中,默认title在右,image在左。然而很多情况下需要不同的布局,比如左右调换,上下布局。这边利用UIEdgeTitle和UIEdgeImage很好的解决了这种场景
IOS 不规则按钮实现方法 直接引用即可
一句代码设置UIButton的image和title的左右上下布局以及image和title的间距,git地址:https://github.com/Coder-ZJ/ZJCustomButton
本文实例为大家分享了IOS使用UIImageView控件制作动画的方法,供大家参考,具体内容如下 先添加40张tomcat的图片到资源列表中:名称为cat_eat0000.jpg到cat_eat0039.jpg。 1、定义所需控件 // 定义按钮,图片控件...
该源码案例是一款不错的类似ios中的UIButton的视图控件,源码AndroidUIView,一个根据ios中的UIButton 自定义的view控件库。只需设置背景图片,所有这些view都会自动的带有按下效果,不需要写selector.xml。