小编Anh*_*ham的帖子

addArrangedSubview 并调整 StackView 的大小

我正在尝试使用按钮将视图添加到 StackView:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var stackOutlet: UIStackView!
    @IBOutlet weak var buttonOutlet: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonAction(_ sender: AnyObject) {
        let newView = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 128))
        newView.heightAnchor.constraint(equalToConstant: 128)
        newView.widthAnchor.constraint(equalToConstant: 240)
        newView.backgroundColor = UIColor.green

        stackOutlet.addArrangedSubview(newView)
        stackOutlet.heightAnchor.constraint(equalToConstant: stackOutlet.frame.size.height …
Run Code Online (Sandbox Code Playgroud)

ios swift uistackview

2
推荐指数
1
解决办法
2759
查看次数

如何在UIScrollView中实现缩放图像?

我的ScrollView运行顺畅.我有资产库中的图像.名称从1开始并结束39.我想放大我的图像.我尝试了不同的方法,但我不能成功.

这是我的代码:

import UIKit

class fotolarView: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var mainScroolView: UIScrollView!

    var imageArray = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()

        mainScroolView.delegate = self
        mainScroolView.frame = view.frame

        imageArray = [#imageLiteral(resourceName: "1"), #imageLiteral(resourceName: "2"), #imageLiteral(resourceName: "3"), #imageLiteral(resourceName: "4"), #imageLiteral(resourceName: "5"), #imageLiteral(resourceName: "6"), #imageLiteral(resourceName: "7"), #imageLiteral(resourceName: "10"), #imageLiteral(resourceName: "8"), #imageLiteral(resourceName: "9"), #imageLiteral(resourceName: "11"), #imageLiteral(resourceName: "12"), #imageLiteral(resourceName: "20"), #imageLiteral(resourceName: "13"), #imageLiteral(resourceName: "21"), #imageLiteral(resourceName: "22"), #imageLiteral(resourceName: "15"), #imageLiteral(resourceName: "23"), #imageLiteral(resourceName: "16"), #imageLiteral(resourceName: "31"), #imageLiteral(resourceName: "24"), #imageLiteral(resourceName: "17"), #imageLiteral(resourceName: "25") , #imageLiteral(resourceName: "32"), …
Run Code Online (Sandbox Code Playgroud)

zooming uiscrollview uiimageview ios swift3

2
推荐指数
1
解决办法
4454
查看次数

UITableViewController 的 didSelectRowAtIndexPath 不起作用

我正在使用它UITableViewController,并覆盖以下方法,并尝试在用户选择不同的行时启用不同的转场。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("You selected row #\(indexPath.row)!")
    switch indexPath.row {
    case 3:
        performSegue(withIdentifier: "segue3", sender: self)
    case 8:
        performSegue(withIdentifier: "segue8", sender: self)
    default:
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

但实际上,这个方法永远不会被调用,并且当我选择一行时,打印输出永远不会显示。

我确实启用了单选,启用了用户交互并将委托和数据源设置为控制器本身(使用时会自动设置UITableViewController,对吧?)

我正在使用静态单元格和默认值UITableViewCell。视图控制器是UITableViewController.

在此输入图像描述

更新

我尝试替换:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) 
Run Code Online (Sandbox Code Playgroud)

和:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
Run Code Online (Sandbox Code Playgroud)

这也行不通。

uitableview ios swift

2
推荐指数
1
解决办法
4958
查看次数

标签(IOS)中的“复制”按钮

从标签复制按钮。

我有计算器的代码,但是我不知道该怎么做,所以当我单击按钮时,带有标签的数字被复制。或如何使它如此显示,以便当我长按它时似乎可以复制:

照片1

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var displayResultLabel: UILabel!
    var stillTyping = false
    var dotIsPlaced = false
    var firstOperand: Double = 0
    var secondOperand: Double = 0
    var operationSign: String = ""

    var currentInput: Double {
        get {
            return Double (displayResultLabel.text!)!
        }
        set {
            let value = "\(newValue)"
            let ValueArray = (value.components(separatedBy:"."))
            if ValueArray[1] == "0" {
                displayResultLabel.text = "\(ValueArray[0])"
            } else {
                displayResultLabel.text = "\(newValue)"
            }
            stillTyping = false
        }
    }

    @IBAction func numberPressed(_ …
Run Code Online (Sandbox Code Playgroud)

ios swift swift3

2
推荐指数
1
解决办法
927
查看次数

在macOS中设置'foregroundColor'时出错

在使用Swift 3.2的macOS项目中,我正在尝试设置UITextView的前景色.

let placeHolderTitleString: NSAttributedString = NSAttributedString(string: "Enter text here", attributes: [NSAttributedStringKey.foregroundColor : NSColor.gray]) // error
Run Code Online (Sandbox Code Playgroud)

我得到的错误是这样的:

类型'NSAttributedStringKey'(又名'NSString')没有成员'foregroundColor'

相同的代码在Swift 4.0项目中工作正常.

我试图根据我发现的iOS Swift 4转换错误的一些答案修改代码- NSAttributedStringKey:任何但我继续得到错误.有没有办法解决这个问题,而无需将项目更新到Swift 4?

macos nsattributedstring swift nsattributedstringkey

2
推荐指数
1
解决办法
744
查看次数

NSDateFormatter错误,一年前返回日期

码:

NSString *ds = @"2013-02-25";
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSDate* date = [formatter dateFromString:ds];
NSLog(@"%@", date); //2012-02-24 16:00:00 +0000
[formatter setDateFormat:@"MMM dd, YYYY"];
NSLog(@"%@", [formatter stringFromDate:date]); //Feb 25, 2012
Run Code Online (Sandbox Code Playgroud)

我做错了什么?还是iOS 6 SDK的bug?

nsdate nsdateformatter ios

1
推荐指数
1
解决办法
240
查看次数

由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArray0 objectAtIndex:]:索引 2 超出空 NSArray 的范围

日志中的错误:

* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[ NSArray0 objectAtIndex:]:索引 2 超出空 NSArray 的范围”*** 第一个抛出调用堆栈:( 0 CoreFoundation 0x000000010fdd8b0b __exceptionPreprocess + 171 1 libobjc.A. dylib 0x00000001143a6141 objc_exception_throw + 48 2 CoreFoundation 0x000000010fdf027d -[__NSArray0 objectAtIndex:] + 93 3 DropInn 0x000000010d598fc4 _TFC7DropInn21ListingViewController9tableViewfTCSo11UITableView 14didSelectRowAtV10Foundation9IndexPath_T_ + 5972 4 DropInn 0x000000010d599837 _TToFC7DropInn21ListingViewController9tableViewfTCSo11UITableView14didSelectRowAtV10Foundation9IndexPath_T_ + 87 5 UIKit 0x00000001122b3dcd -[UI TableView _selectRowAtIndexPath:动画:scrollPosition:notifyDelegate:] + 1763 6 UIKit 0x00000001122b3fe3 -[ UITableView _userSelectRowAtPendingSelectionIndexPath:] + 344 7 UIKit 0x00000001121697f3 _runAfterCACommitDeferredBlocks + 318 8 UIKit 0x00000001121567bc _cleanUpAfterCAFlushAndRunDeferredBlocks + 532 9 UIKit 0x000000011218828c _afterCACommitHandler + 137 10 CoreFoundation …

nsarray ios nsrangeexception swift

1
推荐指数
1
解决办法
8397
查看次数

在Swift 3.1中使用什么代替NSEntityDescription

我是iOS开发的新手.我想在我的应用程序中使用Core Data功能将数据存储在数据库中.

我按照这个核心数据教程.

我在Xcode中编写以下内容:

override func viewDidLoad() {
    super.viewDidLoad()
    initCoredataFunctionalities()
}

func initCoredataFunctionalities() {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let viewContext = appDelegate.managedObjectContext
    let category = NSEntityDescription.insertNewObject(forEntityName:"category",into:viewContext) //this line shows the error of Unresolved symbol in NSEntityDescription
}
Run Code Online (Sandbox Code Playgroud)

你们能给我一个解决方案吗?帮助将不胜感激.

Xcode版本:8.3.3

Swift版本:3.1

core-data ios swift swift3

1
推荐指数
1
解决办法
1589
查看次数

设置iOS导航背景颜色无法正常工作

我有一个非常具体的问题。当我将背景设置为barTintColor蓝色时,它太亮了。我似乎没有做任何事情都能做到100%准确。

因此,我更改了代码,将导航栏背景设置为100%黑色。使用OS X应用程序SIP分析颜色,或者也可以仅将视图设置为黑色,很明显颜色是深灰色,而不是黑色。

是什么使色调变色?就目前而言,我需要的蓝色和导航栏显示的内容并不相同。

    navigationController?.navigationBar.setBackgroundImage(UIImage.imageFromColor(UIColor.black), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()

    navigationController?.navigationBar.barStyle = .blackOpaque
    navigationController?.navigationBar.isOpaque = true
    navigationController?.navigationBar.barTintColor = UIColor.black
Run Code Online (Sandbox Code Playgroud)

同样在一个空白项目中,同样的问题。

ios swift

0
推荐指数
1
解决办法
949
查看次数

从数组列表中随机选择5个元素,而不重复元素

我目前正在尝试为iOS制作应用程序,但我无法获得一些简单的代码.基本上我需要从数组列表中随机选择5个元素而不重复元素.我有一个草稿,但它只显示一个元素.

这是我的代码:

let array1 = ["salmon", "turkey", "salad", "curry", "sushi", "pizza"]

let randomIndex1 = Int(arc4random_uniform(UInt32(array1.count)))

print(array1[randomIndex1])
Run Code Online (Sandbox Code Playgroud)

arrays random swift swift3

0
推荐指数
1
解决办法
1736
查看次数