GVKun编程网logo

Swift UIButton 和 UILabel 的详细解释和创建使用(swift uitableview)

3

针对SwiftUIButton和UILabel的详细解释和创建使用和swiftuitableview这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展IOS7.1兼容性bug问题,UIButt

针对Swift UIButton 和 UILabel 的详细解释和创建使用swift uitableview这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展IOS 7.1 兼容性 bug 问题,UIButton,UILabel ...、iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts、iOS UIButton – UIButton setUserInteractionEnabled和setEnabled之间的区别、OC 中 UILabel、UITextField 以及 UIButton 的用法总结等相关知识,希望可以帮助到你。

本文目录一览:

Swift UIButton 和 UILabel 的详细解释和创建使用(swift uitableview)

Swift UIButton 和 UILabel 的详细解释和创建使用(swift uitableview)


/*

App 的开发过程中,我们都不会错过的两个类 UIButton UILabel

其中 UIButton 是用户和客户端的交互连接员 ,处于重要的地位

然而,交互总有效果吧,那效果怎么显示呢,那就的UILabel 上场了

本文重点:

主要介绍 UIbutton UILabel 的创建和类型(Btn);以及它们的

常用属性

*/

// Created by 周双建 on 15/12/1.

// copyright © 2015 周双建. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


override func viewDidLoad() {

super.viewDidLoad()

/***********************************************************/


//创建一个Button

let ZSJBtn = UIButton(type: UIButtonType.Custom) as UIButton

/*

let ZSJBtnOne = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

这种方法已经废弃,创建 Btn 使用上面的方法

*/

//给按钮设置尺寸

ZSJBtn.frame = CGRectMake(50, 20,self.view.frame.size.width-100,100)

//给按钮一个背景

//1、给定色彩

ZSJBtn.backgroundColor = UIColor.redColor()

//2、用图片填色

ZSJBtn.backgroundColor = UIColor(patternImage: UIImage(named: "fadcf1d10901b800bd13b745d48e5755.jpg")!)

//3、对Btn 进行裁角 true 是允许裁剪 false 不允许裁剪

ZSJBtn.layer.masksToBounds = true

// 设置裁剪的半径

ZSJBtn.layer.cornerRadius = 10

/***********************************************************/

/*

contentsScale

contentsScale属性定义了寄宿图的像素尺寸和视图大小的比例,默认情况下它是一个值为1.0的浮点数。

contentsScale的目的并不是那么明显。它并不是总会对屏幕上的寄宿图有影响。如果你尝试对我们的例子设置不同的值,你就会发现根本没任何影响。因为contents由于设置了contentsGravity属性,所以它已经被拉伸以适应图层的边界。

*/

ZSJBtn.layer.contentsScale = 20

/***********************************************************/

//给按钮设置图片 背景图片

//1、首先我们要清楚上面的 背景颜色

ZSJBtn.backgroundColor = UIColor.clearColor()

//2、图片

ZSJBtn.setimage(UIImage(named: "fadcf1d10901b800bd13b745d48e5755.jpg"),forState:UIControlState.disabled)

//3、设置背景图片

ZSJBtn.setBackgroundImage(UIImage(named: "fadcf1d10901b800bd13b745d48e5755.jpg"),forState: UIControlState.normal)

/*

这两种设置Btn 的背景图片和图片,的结果是一样的。都能充满Btn 的尺寸

*/

//设置Btn 的字体

//1、默认状态

ZSJBtn.setTitle("成功",forState: UIControlState.normal)

//2、选中状态

ZSJBtn.setTitle("富有",forState: UIControlState.Selected)

//3、突出高亮的状态

ZSJBtn.setTitle("奋斗",forState: UIControlState.Highlighted)

//4、不可用的状态

ZSJBtn.setTitle("抱怨",forState: UIControlState.disabled)

//5、设置字体的颜色 也四种状态

ZSJBtn.setTitleColor(UIColor.redColor(),0)"> //设置字体的大小 和名字

ZSJBtn.titleLabel!.font = UIFont(name: "Zapio",size: 20)

//给字体设置阴影 颜色

ZSJBtn.titleLabel!.shadowColor = UIColor.purpleColor()

//设置阴影字体的偏移量

ZSJBtn.titleLabel!.shadowOffset = CGSizeMake(10,0)

//设置多行字体 0 代表无限制

ZSJBtn.titleLabel!.numberOfLines = 0

/***********************************************************/

//设置标签

ZSJBtn.tag = 100

// 点击事件 不带参数

ZSJBtn.addTarget(self,action: "Click",forControlEvents: UIControlEvents.TouchUpInside)

//带参数的 点击事件

ZSJBtn.addTarget(self,action: "SEL:",forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(ZSJBtn)

// Do any additional setup after loading the view,typically from a nib.

}

func SEL(btn:UIButton){

self.Click()

print("jk",(btn.tag))

}

func Click(){

//创建 UILabel

let label = UILabel(frame: CGRectMake(50,200,50))

// Label 的赋值

label.text = "思念是种痛"

//Label 颜色

label.textColor = UIColor.magentaColor()

//设置字体加粗

label.font = UIFont.boldSystemFontOfSize(20)

// 设置Label的行数

label.numberOfLines = 0

//设置阴影

label.shadowOffset = CGSizeMake(15, 2)

label.shadowColor = UIColor.blackColor()

self.view.addSubview(label)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// dispose of any resources that can be recreated.

}



}

IOS 7.1 兼容性 bug 问题,UIButton,UILabel ...

IOS 7.1 兼容性 bug 问题,UIButton,UILabel ...

高春辉、王春生、朱峰:关于开源创业的 15 件小事

  前面我们测试都通过的程序,昨天我修复了几个 bug 后做回归测试,突然发现了好几个 bug,瞬间吓坏我了,差点就还原了版本,但我还是耐着性子找了,最后,也就是今天上午,我才定定位出问题所在,原来是升级 IOS 7.1 后,系统默认的好多组件啥的,逻辑方式发生下改变,也就是出现了些兼容性问题。

    我发现的这个问题是 UIButton 的,我发现,当给一个 button 的 enable 设置为 NO 后,如果想再更新它的 title,background,就没法更新。

兼容方案是:先恢复 enable 为 YES,然后再更新,最后再还原。

类似代码:

/**
 *传递实体和序号
 **/
-(void)setEntity:(TripNode *)dataEntity andDayIndex:(int)index
{
	self.dataEntity = dataEntity;
	NSLog(@"index :%d",index);
	self.leftOperationButton.enabled = YES;
	if (self.style == TPUpdateTripBranchHeaderStyleTo) {
			[self.leftOperationButton setTitle:[NSString stringWithFormat:@"D%d",index] forState:UIControlStateNormal];
			self.leftOperationButton.enabled = NO;
	}else if (self.style == TPUpdateTripBranchHeaderStyleAdd) {
		[self.leftOperationButton setTitle:@"" forState:UIControlStateNormal];
	} else if (self.style == TPUpdateTripBranchHeaderStyleFrom) {
		 [self.leftOperationButton setTitle:@"From" forState:UIControlStateNormal];
		self.leftOperationButton.enabled = NO;
	}else if (self.style == TPUpdateTripBranchHeaderStyleReturn) {
		 [self.leftOperationButton setTitle:@"Return" forState:UIControlStateNormal];
		self.leftOperationButton.enabled = NO;
		
	}
}

其他的几个兼容性问题我就不一一列举了,上 stackoverflow 上一搜关键字:

ios 7.1 not working

你就会发现一大大堆的类似问题:http://stackoverflow.com/search?q=ios+7.1+not+working

具体解决方案,自己根据个人情况找吧,祝你们好运!!




iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>

iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts

如何解决iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>

我不断收到来自随机用户的随机崩溃报告。不幸的是,我无法定期重现这一点。用户说崩溃是在 discussionViewController 中随机发生的。所有崩溃报告都有类似的内容:

0   libswiftCore.dylib              0x00000001a53face4 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>,unsigned int) + 60 (atomic:1003)
1   libswiftCore.dylib              0x00000001a53c59e0 swift_retain + 124 (RefCount.h:813)
2   libswiftCore.dylib              0x00000001a5401d60 swift_bridgeObjectRetain + 56 (SwiftObject.mm:585)
3   APPNAME                             0x0000000102b59734 closure #1 in discussionViewController.fetchPostData() + 7916

这是完整的崩溃日志和崩溃的线程:

Hardware Model:      iphone11,6
Process:             APPNAME [11770]
Path:                /private/var/containers/Bundle/Application/.../APPNAME.app/APPNAME
Identifier:          ----
Version:             62 (62)
AppStoretools:       12E262
AppVariant:          1:iphone11,6:13
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           ---- [1824]


Date/Time:           2021-06-17 12:07:01.4346 +1000
Launch Time:         2021-06-17 12:06:56.4993 +1000
OS Version:          iPhone OS 14.6 (18F72)
Release Type:        User
Baseband Version:    3.04.01
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x8000000000000010 -> 0x0000000000000010 (possible pointer authentication failure)
VM Region Info: 0x10 is not in any region.  Bytes before following region: 4339515376
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   102a7c000-102a94000 [   96K] r-x/r-x SM=COW  ...APPNAME.app/APPNAME

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL,Code 0xb
Terminating Process: exc handler [11770]
Triggered by Thread:  3


Thread 3 name:
Thread 3 Crashed:
0   libswiftCore.dylib              0x00000001a53face4 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>,unsigned int) + 60 (atomic:1003)
1   libswiftCore.dylib              0x00000001a53c59e0 swift_retain + 124 (RefCount.h:813)
2   libswiftCore.dylib              0x00000001a5401d60 swift_bridgeObjectRetain + 56 (SwiftObject.mm:585)
3   APPNAME                             0x0000000102b59734 closure #1 in discussionViewController.fetchPostData() + 7916
4   APPNAME                             0x0000000102ad09d4 thunk for @escaping @callee_guaranteed (@guaranteed Data?,@guaranteed NSURLResponse?,@guaranteed Error?) -> () + 132 (<compiler-generated>:0)
5   CFNetwork                       0x00000001a1b0a3dc __40-[__NSURLSessionLocal taskForClassInfo:]_block_invoke + 540 (LocalSession.mm:687)
6   CFNetwork                       0x00000001a1b1c768 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 244 (LocalSessionTask.mm:584)
7   libdispatch.dylib               0x00000001a10d1a84 _dispatch_call_block_and_release + 32 (init.c:1466)
8   libdispatch.dylib               0x00000001a10d381c _dispatch_client_callout + 20 (object.m:559)
9   libdispatch.dylib               0x00000001a10db004 _dispatch_lane_serial_drain + 620 (inline_internal.h:2557)
10  libdispatch.dylib               0x00000001a10dbc34 _dispatch_lane_invoke + 456 (queue.c:3862)
11  libdispatch.dylib               0x00000001a10e64bc _dispatch_workloop_worker_thread + 764 (queue.c:6589)
12  libsystem_pthread.dylib         0x00000001ed04a7a4 0x1ed047000 + 14244
13  libsystem_pthread.dylib         0x00000001ed05174c 0x1ed047000 + 42828

我已验证 discussionViewController.fetchPostData() 不会强制解开任何可选选项,没有 try! 并且在任何地方都使用 [weak self]self?。该函数非常大,所以我很难缩小崩溃发生的范围。

iOS UIButton – UIButton setUserInteractionEnabled和setEnabled之间的区别

iOS UIButton – UIButton setUserInteractionEnabled和setEnabled之间的区别

等等!!!我知道你可能会认为这个问题已经被问及了几次.但我可以保证你这个问题是独一无二的.

问题:在iOS应用程序中,只要想象有两个按钮,如下图所示,它们有两个动作,就像一个切换逻辑.

它的逻辑可能如下:

- (IBAction)testBtnClicked:(id)sender {
    if ([self.testBtn isEnabled]) {
        [self.testBtn setEnabled:NO];
        [self.setInteractionBtn setUserInteractionEnabled:YES];
    } else {
        [self.testBtn setEnabled:YES];
        [self.setInteractionBtn setUserInteractionEnabled:NO];
    }
}

- (IBAction)setInteractionBtnClicked:(id)sender {
    if ([self.setInteractionBtn isEnabled]) {
        [self.setInteractionBtn setUserInteractionEnabled:NO];
        [self.testBtn setEnabled:YES];
    } else {
        [self.setInteractionBtn setUserInteractionEnabled:YES];
        [self.testBtn setEnabled:NO];
    }
}

所以我没有看到setEnabled方法和setUserInteractionEnabled方法有很大的区别.它们的行为类似于阻止用户不允许使用它的单一方法.但是,如果同样一样,即使setUserInteractionEnabled设置为False,我们如何能够检测到isEnabled true或false?

以下是使这个问题成为SO的另一个问题的可能重复的原因:

>即使一些排名较高的代码可能将我的问题标记为可能的重复,Q& A没有给我正确的理解.
正如@danh所说,

At least one reason is that during animation,user interaction is
disabled on UIViews. It would be wrong for controls to draw themselves
as greyed out while they are animated. So at least during animation,
the two properties have distinct meanings.
Gave me the real answer or the reason to see that these two methods are for two reasons. Because anyone Could say that setUserInteractionEnabled doesn’t do changes on UI state,but at least only on @danh’s answer had first stated that it might be implicitly used during UI Animations.

解决方法

他们几乎一样. userInteractionEnabled是UIView的一个属性,用于切换视图是否接收任何用户触摸. enabled是UIControl的属性(它是UIView的一个子类和UIButton的超类),并且具有相同的效果.一个区别是,UIKit控件可能会根据启用状态绘制不同的内容,而抽象UIView不是这样.

好的,那为什么?

由于UIControl子类继承两者,为什么有两个几乎相同的属性?为什么不控制只是放弃“启用”的想法,并根据userInteractionEnabled状态绘制不同的内容?

至少有一个原因是在动画中,UIViews上的用户交互被禁用.控件在动画时将自己画成灰色是错误的.所以至少在动画时,这两个属性有不同的含义.

OC 中 UILabel、UITextField 以及 UIButton 的用法总结

OC 中 UILabel、UITextField 以及 UIButton 的用法总结

一、UILable (父类:UIView)

 1. 初始化:

 UILabel *label = [UILabel alloc]initWithFrame:CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)//注意这里的初始化方法是它的父类UIView的方法,它本身是没有初始化方法的

2. 有关文本的操作

label.text = @"i love you";//在label上面添加文字
label.textColor = [UIColor blueColor];//设置label文字的颜色
label.font = [UIFont systemFontOfSize:20];//设置label文字的字体为20,这里系统默认的字体大小为17
label.font = [UIFont fontWithName:@"Telugu Sangam MN-Bold" seze:20];//设置label的文字的字体为20,字体名字为Telugu Sangam MN,而后面加-Bold是使字体加粗,注意有些字体是不能加粗的
label.textAlignment = NSTextAlignmentCenter;//居中对齐,还有左对齐和右对齐,可以点进头文件查看这个枚举值
label.lineBreakMode = NSLineBreakByTruncatingTail;//这种断行方式为abcd...,还有其它的也可以进去头文件查看
[label sizeToFit]//label大小自适应文字和图片
label.clipsToBounds = YES;//超出边框范围是否裁剪
label.shadowColor = [UIColor redColor];//设置阴影颜色
label.shadowOffset = CGSizeMake(2,3);//设置阴影程度

3. 有关 label 尺寸大小的操作

 label.layer.borderColor = [UIColor blackColor];//设置边框颜色为黑色
 label.layer.borderWidth = 1;//label的边框宽度,一般设置为2以下
 label.layer.cornerRadius = 5;//设置圆边框,这里要想把label变为圆形,必须使label.clipsToBounds = YES;

4. 用户能否交换的设置

label.userInteractionEnabled = YES;设置之后(默认为NO),label也会有响应传递,这样不至于将响应链断掉

5. 有关颜色和图片的操作

label.backgroundColor = [UIColor redColor];//设置label的背景颜色为红色
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123.jpg"]];//将123.jpg图片加到label上作为背景
//也可以将其他的viw如:UIImageView *image作为它的子视图,从而添加上去,如:
[lable addSubView:image];


二、UITextField (父类:UIControl , 父父类:UIView)

1. 初始化

UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(305020025)];//也是自身没有初始化方法,更没有便利构造器,利用父类的初始化方法初始化

2. 输入控制

textfield.placeholder = @"请输入银行卡密码";//占位字符串,提示用户输入什么内容,一旦输入则消失
textfield.secureTextEnTry = YES;//输入显示为密文,一般用于密码输入
textfield.keyboardType = UIKeyboardTypeEmailAddress;//设置输入时键盘的样式,这里设置为输入邮箱的键盘,会有@字符
textfield.clearButtonMode = UITextFieldViewModeAlways;//设置右侧的清除按钮
textfield.enabled = YES;//是否允许编辑,默认为YES

3. 外观控制

textfield.borderStyle = UITextBorderStyleRoundedRect;//设置为系统自带的圆角
textfield.layer.borderColor = [UIColor lightGrayColor].CGColor//修改textfield的边框颜色
textfield.layer.borderWidth = 1;//设置边框宽度
textfield.layer.cornerRadius = 6;//设置边框圆角半径,对于上面的这三个边框的属性,只要是UIView的子类都可以使用
textfield.returnKeyType = UIReturnKeyDone;//设置键盘return键的样式
textfield.leftView = view;//其中view是UIView的对象,设置texfield的左视图

textfield.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(00030)];
textfield.inputAccessoryView.backgroundColor = [UIColor greenColor];//输入的辅助视图,也就是键盘上面的颜色块

4. 协议方法以及控制

     UITextField 里面有个 UITextFieldDelegate 协议,协议里写的方法是有关输入控制响应的,在 textfield 又输入时,会自动调用这些方法,为了达到我们想要的输入效果,让根视图控制器签订这个协议,改写其中的方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];//取消第一响应者,回收键盘
    textFieldNext = [self.view viewWithTag:textField.tag + 1];//使得下一个textField作为第一响应者,也就是当你按下return键就自动跳转到下一个textField进行输入
    return YES;
}
//结束编辑时自动调用
-(void)textFieldDidEndEditing:(UITextField *)textField//text
{
    if ([textField.text isEqualToString:@"123456"])
    {
        NSLog(@"密码正确");
    }
    else
        NSLog(@"密码错误");
}
//
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string//在range范围内替换string
{
    NSLog(@"%@ %@",string,NSStringFromRange(range));//将结构体转换为字符串
    NSString *str = [textField.text stringByAppendingString:string];
    
    if (str.length > 5)//还要加上刚刚输入的进行判断
    {
        textField.text = [str substringToIndex:5];
        
        return NO;
    }
    return YES;
}


三.UIButton (父类 UICtrol,父父类 UIView)


1. 初始化 

UIButton *button = [UIBtton buttonWithType:UIButtonTypeSystem]//这里的UIButtonTypeSystem是button的类型
button.frame = CGRectMake(15220300200);//设置button的大小

2. 文本操作和颜色操作

button.backgroundColor = [UIColor redColor];//把它的背景色设置为红色
[button  setTitle:@"点我" forState:UIControlStateNormal];//设置button里面的文字和其格式
[button setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];//设置button的字体颜色
button.tintColor = [UIColor blackColor];//给button着色
[button setBackgroudImage:[UIImage imageNamed:@"123.png"] forState:UIControlStateNormal];//给button设置背景图片
button.titleLabel.font = [UIFont systemFontOfSize:20];//将botton上面的字体设置为20,这里可以看出在button上面加了一个UILable对象

3. 点击响应

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];//如果出现点击时间(点击方式为:UIControlEventTouchUpInside),就调用self的buttonAction方法,经常会用来响应button





关于Swift UIButton 和 UILabel 的详细解释和创建使用swift uitableview的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于IOS 7.1 兼容性 bug 问题,UIButton,UILabel ...、iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts、iOS UIButton – UIButton setUserInteractionEnabled和setEnabled之间的区别、OC 中 UILabel、UITextField 以及 UIButton 的用法总结的相关知识,请在本站寻找。

本文标签: