在swift中禁止从UITextField闪烁光标?

时间:2016-09-15 05:46:15

标签: ios swift uitextfield swift3

如何从UITextField禁用Swift中的游标?条件 - 用户登录时,进入应用程序。如果用户可以从应用程序注销,则不将光标显示到UITextField。

5 个答案:

答案 0 :(得分:22)

如果您不想package com.project.backgroundprocesstest; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationManager; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; import org.json.JSONObject; import java.io.IOException; import java.util.Date; import java.util.List; import java.util.Locale; /** * Created by Alex on 14/09/2016. */ public class LocationControl extends AppCompatActivity{ final int REQUEST_ACCESS_FINE_LOCATION = 5; private Context context; private LocationManager locationManager; public LocationControl(Context context){ this.context = context; this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if(ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Log.v("SPAM", "1"); this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new MyLocationListener(context)); } } public LocationManager getLocationManager(){ return locationManager; } public String getLocation(){ Log.v("SPAM", "1"); if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.v("CheckPermission","Permission Denied"); ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_ACCESS_FINE_LOCATION); Log.v("CheckPermission","It Does run"); } else{ Log.v("CheckPermission","Permission Granted"); Location location = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); double longitudinal = location.getLongitude(); double latitudinal = location.getLatitude(); String cityName = findCity(location); Date date = new Date(); return setJSON(longitudinal,latitudinal,cityName,date); } return null; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults){ switch(requestCode){ case REQUEST_ACCESS_FINE_LOCATION: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { Log.d("CheckPermission","User Denisd Permission"); } return; } } } public String setJSON(double lng, double lat, String place, Date date) { JSONObject jsonObject = null; try { jsonObject = new JSONObject(); jsonObject.put("longitudinal", lng); jsonObject.put("latitudinal", lat); jsonObject.put("name", place); jsonObject.put("created", date); } catch (Exception e) { Log.e("Error", "JSON ERROR"); } if(jsonObject != null) return jsonObject.toString(); else return null; } private String findCity(Location loc){ String cityName = null; Geocoder gcd = new Geocoder(context, Locale.getDefault()); // IF any addresses which belong to the Longitudinal and Latitudinal are found, // set cityName and print it List<Address> addresses; try{ addresses = gcd.getFromLocation(loc.getLatitude(),loc.getLongitude(),1 ); if(addresses.size()>0){ System.out.println(addresses.get(0).getLocality()); cityName = addresses.get(0).getLocality(); } }catch(IOException e){ e.printStackTrace(); } return cityName; } private String locationToString(Location location){ return Location.convert(location.getLatitude(),Location.FORMAT_DEGREES)+" " + Location.convert(location.getLongitude(),Location.FORMAT_DEGREES); } } ,则可以将cursor设置为tintColor。在这种情况下,textField将处于活动状态。如果您想将其设为无效字段,则应调用clearColor的{​​{1}}方法!

答案 1 :(得分:9)

对于适用于我的Swift 4:

textField.tintColor = UIColor.clear

答案 2 :(得分:6)

最初在user default设置一些bool值,当用户按下注销按钮时将值设置为NO

例如

UserDefaults.standard().set(true, forKey: "disalert")

当你来到这个页面时检查条件如

let disalert: Bool = UserDefaults.standard().bool(forKey: "disalert")

      self.youtextField().tintColor = UIColor.blueColor()
    if disalert {
        self.youtextField().tintColor = UIColor.clearColor()
        self.youtextField().resignFirstresponder()
     }

当用户按下登录按钮时,将用户默认值设置为

UserDefaults.standard().set(false, forKey: "disalert")

使用

(self.youtextField.valueForKey("textInputTraits") as! String)["insertionPointColor"] = UIColor.clearColor()

或设置为

 self.youtextField().tintColor = UIColor.clearColor()

答案 3 :(得分:3)

首先,您需要确认UITextFieldDelegate,然后将textField设置为yourTextField.delegate =self 加  yourTextField.resignFirstResponder()

代码。将resignFirstResponder调用yourTextField。 你也可以使用下面的代码(根据你的需要)。

在委托方法textFieldDidEndEditing(textField: UITextField)中检查(if)是否有某些值/文本不是。

yourTextField.attributedPlaceholder = NSAttributedString(string: "User Name", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()]) textUserName.resignFirstResponder()

答案 4 :(得分:0)

您可以更改tintcolor

self.youtextField()。tintColor = UIColor.clearColor()

您为文本字段创建了一个课程