iOS开发之UIButton

UIButton是iOS开发中非常基础也经常用到的控件。我在此总结一下它的一些基本用法,提醒自己的同时也以飨读者。

一、UIButton基础用法

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20,20, 100, 40)];
    btn.backgroundColor = [UIColor redColor]; //背景颜色
    [btn setTitle:@"我是一个按钮" forState:UIControlStateNormal]; //标题title
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; //标题title颜色
    btn.titleLabel.font = [UIFont systemFontOfSize:20]; //标题title字体大小
    [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside]; //点击事件

二、UIButton特殊用法

2.1标题title位置。

     1.标题默认为居中,如果设为局左   

     btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
     2.上面的方法是靠近左边框,如果想距左边框一些距离

     btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

     btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

2.2设置圆角UIButton

    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 6; //弧度值

2.3设置UIButton边框

    btn.layer.borderColor = [[UIColor whiteColor] CGColor];
    btn.layer.borderWidth = 1.0f;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中实现Twitter登录功能的Demo可以按照以下步骤进行: 1. 首先,在Twitter开发者网站上创建一个应用,获取应用的API Key和Secret Key,并将它们添加到项目的Info.plist文件的URL schemes和URL whitelist中。 2. 在AppDelegate中导入Twitter框架并添加以下代码: ```swift import TwitterKit func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { TWTRTwitter.sharedInstance().start(withConsumerKey: "YOUR_API_KEY", consumerSecret: "YOUR_API_SECRET") return true } ``` 3. 在需要实现登录的ViewController中导入Twitter框架并添加以下代码: ```swift import TwitterKit @IBAction func twitterLoginButtonTapped(_ sender: UIButton) { TWTRTwitter.sharedInstance().logIn { (session, error) in if (session != nil) { print("Twitter login successful with user ID: \(session!.userID)") } else { print("Twitter login error: \(error!.localizedDescription)") } } } ``` 在上述代码中,当用户点击Twitter登录按钮时,调用`TWTRTwitter.sharedInstance().logIn`方法会打开Twitter登录界面,用户在该界面上登录成功后,会返回一个session,其中包含用户的userID和token等信息。根据返回的session是否为nil,判断登录是否成功。 注意:在使用此Demo之前,需要在项目的Build Settings中的Other Linker Flags添加`-ObjC`标志,否则可能会出现链接错误。 以上就是实现Twitter登录功能的iOS开发Demo。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值