为UISearchBar textField添加底部边框

时间:2018-08-19 09:57:36

标签: ios swift uitextfield border uisearchbar

我在UISearchBar的titleView中添加了navigationItem。 我想在UISearchBar的textField中添加底部边框。

尝试添加图层时,没有任何显示。

extension UITextField {

func underlined() {

         let border = CALayer()
         let width = CGFloat(3.0)
         border.borderColor = UIColor.red.cgColor
         border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height: width)
         border.borderWidth = width
         self.layer.addSublayer(border)
         self.layer.masksToBounds = true

    }

}

我尝试使用此代码来获取textField的内容,但是没有运气。

let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField

textFieldInsideSearchBar?.borderStyle = .none
textFieldInsideSearchBar?.textColor = UIColor.white
textFieldInsideSearchBar?.underlined()

我们将不胜感激!

2 个答案:

答案 0 :(得分:2)

那是因为在viewDidLoad方法中,帧尚未正确配置,请尝试以下操作:

    using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERL.Gateway;
using ERL.Models;

namespace ERL
{
    public partial class LabEntryUI : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
       public static void SaveLabData(List<LabEntry> labEntryArray)
        {
            string constr = ConfigurationManager.ConnectionStrings["ERLConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (
                    SqlCommand Command =
                        new SqlCommand("Insert into LabReport Values(@unitName,@sampleName,@entryDate,@entryTime," +
                                       "@userId,@density15C,@rvpPsi,@colourAstm,@fp,@viscosity50," +
                                       "@viscosity100,@pp,@astmIvp,@astm5,@astm10,@astm50,@astm90,@astm95,@fbp,@bswb4Oxid," +
                                       "@bswAfterOxid,@report,@fr5Xylene)"))
                {
                    Command.Parameters.Clear();
                    Command.Parameters.Add("unitName", SqlDbType.NChar);
                    Command.Parameters.Add("sampleName", SqlDbType.VarChar);
                    Command.Parameters.Add("entryDate", SqlDbType.DateTime);
                    Command.Parameters.Add("entryTime", SqlDbType.NChar);
                    Command.Parameters.Add("userId", SqlDbType.NChar);

                    Command.Parameters.Add("density15C", SqlDbType.Decimal);
                    Command.Parameters.Add("rvpPsi", SqlDbType.Decimal);
                    Command.Parameters.Add("colourAstm", SqlDbType.VarChar);
                    Command.Parameters.Add("fp", SqlDbType.Decimal);
                    Command.Parameters.Add("viscosity50", SqlDbType.Decimal);

                    Command.Parameters.Add("viscosity100", SqlDbType.Decimal);
                    Command.Parameters.Add("pp", SqlDbType.Decimal);
                    Command.Parameters.Add("astmIvp", SqlDbType.Decimal);
                    Command.Parameters.Add("astm5", SqlDbType.Decimal);
                    Command.Parameters.Add("astm10", SqlDbType.Decimal);

                    Command.Parameters.Add("astm50", SqlDbType.Decimal);
                    Command.Parameters.Add("astm90", SqlDbType.VarChar);
                    Command.Parameters.Add("astm95", SqlDbType.VarChar);
                    Command.Parameters.Add("fbp", SqlDbType.Decimal);
                    Command.Parameters.Add("bswb4Oxid", SqlDbType.Decimal);

                    Command.Parameters.Add("bswAfterOxid", SqlDbType.Decimal);
                    Command.Parameters.Add("report", SqlDbType.VarChar);
                    Command.Parameters.Add("fr5Xylene", SqlDbType.Decimal);


                    foreach (var labEntry in labEntryArray)
                    {
                        Command.Parameters["unitName"].Value = labEntry.unitname;
                        Command.Parameters["sampleName"].Value = labEntry.SampleName;
                        Command.Parameters["entryDate"].Value = labEntry.Entrydate;
                        Command.Parameters["entryTime"].Value = labEntry.EntryTime;
                        Command.Parameters["userId"].Value = "";
                        Command.Parameters["density15C"].Value = labEntry.Density15C;

                        Command.Parameters["rvpPsi"].Value = labEntry.RVP_PSI;
                        Command.Parameters["colourAstm"].Value = labEntry.colourASTM;
                        Command.Parameters["fp"].Value = labEntry.FP;
                        Command.Parameters["viscosity50"].Value = labEntry.Viscosity50;
                        Command.Parameters["viscosity100"].Value = labEntry.Viscosity100;
                        Command.Parameters["pp"].Value = labEntry.pp;
                        Command.Parameters["astmIvp"].Value = labEntry.ASTM_IBP;
                        Command.Parameters["astm5"].Value = labEntry.ASTM5;
                        Command.Parameters["astm10"].Value = labEntry.ASTM10;
                        Command.Parameters["astm50"].Value = labEntry.ASTM50;
                        Command.Parameters["astm90"].Value = labEntry.ASTM90;
                        Command.Parameters["astm95"].Value = labEntry.ASTM95;
                        Command.Parameters["fbp"].Value = labEntry.FBP;
                        Command.Parameters["bswb4Oxid"].Value = labEntry.BSWB4Oxid;
                        Command.Parameters["bswAfterOxid"].Value = labEntry.BSWAfterOxid;
                        Command.Parameters["report"].Value = labEntry.Report;
                        Command.Parameters["fr5Xylene"].Value = labEntry.FR5xylene;

                        Command.Connection = con;
                        con.Open();

                        Command.ExecuteNonQuery();

                        con.Close();
                    }
                }
            }


        }
    }
}

答案 1 :(得分:1)

将代码放入viewDidAppear(:)方法(而不是viewDidLoad(:))中以读取搜索栏的文本字段和setUnderline。

问题在于您要在获取textField子图层之前设置它的框架。尝试打印文本字段的框架,您会发现(0,0,0,0)导致子层的y位置为零。因此,请在视图出现后进行设置,以使其边界和y位置均符合所需。